Results 1 to 4 of 4

Thread: How to push array like this?

  1. #1
    Join Date
    Aug 2008
    Location
    karanganyar, solo, indonesia
    Posts
    161
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Question How to push array like this?

    PHP Code:
    $day_array = array();
    foreach (
    range(311)as $day){
        
    array_push($day_array$day);

    if i write a code like above, the $day_array will return like this:
    Array (
    [0] => 31
    [1] => 30
    [2] => 29
    [3] => 28
    and so on...

    is there a solution to make 'my array key' and 'my key value' has the same value like this one
    $day_array = array(
    '31' => '31',
    '30' => '30',
    '29' => '29'
    and so on...
    );

    thanks.. i need help...
    ///////////////////////////////////////////////////
    ///// http://www.mediatutorial.web.id
    ///////////////////////////////////////////////////

  2. #2
    Join Date
    Apr 2008
    Location
    So.Cal
    Posts
    3,643
    Thanks
    63
    Thanked 516 Times in 502 Posts
    Blog Entries
    5

    Default

    PHP Code:
    $day_array = array();
    foreach (
    range(311)as $day){
        
    $day_array[$day] = $day;


  3. #3
    Join Date
    Aug 2008
    Location
    karanganyar, solo, indonesia
    Posts
    161
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default

    Thanks so much, i will try it...
    ///////////////////////////////////////////////////
    ///// http://www.mediatutorial.web.id
    ///////////////////////////////////////////////////

  4. #4
    Join Date
    Jan 2011
    Posts
    50
    Thanks
    0
    Thanked 1 Time in 1 Post

    Default

    <?php
    $arr = array(1, 2, 3, 4);
    foreach ($arr as &$value) {
    $value = $value * 2;
    }
    // $arr is now array(2, 4, 6, 8)
    unset($value); // break the reference with the last element
    ?>


    This is possible only if iterated array can be referenced (i.e. is variable), that means the following code won't work:

    <?php
    foreach (array(1, 2, 3, 4) as &$value) {
    $value = $value * 2;
    }

    ?>

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •