ID:               49347
 User updated by:  zoli at lippai dot net
 Reported By:      zoli at lippai dot net
 Status:           Bogus
 Bug Type:         Arrays related
 Operating System: OS X 10.5.8
 PHP Version:      5.3.0
 New Comment:

Here is an other example:

$list = array(
        array('id' => 1),
        array('id' => 2)
);
for($i = 0; $i < count($list); $i++) {
        $item = &$list[$i];
        $item['someValue'] = 'x';
}
var_dump($list);

after that the value of $list is:

array(2) {
  [0]=>
  array(2) {
    ["id"]=>
    int(1)
    ["someValue"]=>
    string(1) "x"
  }
  [1]=>
  &array(2) {
    ["id"]=>
    int(2)
    ["someValue"]=>
    string(1) "x"
  }

The second element has a & in front of it, I think this is causing the

problem.


Previous Comments:
------------------------------------------------------------------------

[2009-08-24 20:36:35] [email protected]

Thank you for taking the time to write to us, but this is not
a bug. Please double-check the documentation available at
http://www.php.net/manual/ and the instructions on how to report
a bug at http://bugs.php.net/how-to-report.php



------------------------------------------------------------------------

[2009-08-24 18:14:50] [email protected]

The variable $list has the expected value after the for loop. You can
show this by doing print_r($list). However, the $item in the first for
loop seems to interfere with the $item in the second foreach loop.

------------------------------------------------------------------------

[2009-08-24 15:44:05] zoli at lippai dot net

Description:
------------
When I iterate through an array with "for" and create a reference to
the current element inside the for, then the last element of the array
will be a reference to the previous element.
If I unset the reference variable at the end of every cycle, then it
works fine.

Reproduce code:
---------------
<?php
$list = array(
        array('id' => 1),
        array('id' => 2)
);
for($i = 0; $i < count($list); $i++) {
        $item = &$list[$i];
        $item['someValue'] = 'x';
}
foreach($list as $item)
{
        var_dump($item);
}

Expected result:
----------------
array(2) {
  ["id"]=>
  int(1)
  ["someValue"]=>
  string(1) "x"
}
array(2) {
  ["id"]=>
  int(2)
  ["someValue"]=>
  string(1) "x"
}


Actual result:
--------------
array(2) {
  ["id"]=>
  int(1)
  ["someValue"]=>
  string(1) "x"
}
array(2) {
  ["id"]=>
  int(1)
  ["someValue"]=>
  string(1) "x"
}

(Please note the second elements id, which is 1 instead of 2)


------------------------------------------------------------------------


-- 
Edit this bug report at http://bugs.php.net/?id=49347&edit=1

Reply via email to