ID:               49347
 Updated by:       [email protected]
 Reported By:      zoli at lippai dot net
 Status:           Open
 Bug Type:         Arrays related
 Operating System: OS X 10.5.8
 PHP Version:      5.3.0
 New Comment:

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.


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

[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