ID: 46123 Updated by: [EMAIL PROTECTED] Reported By: martin dot akesson at qbrick dot com -Status: Open +Status: Bogus Bug Type: Arrays related Operating System: FreeBSD 7.0 PHP Version: 5.2.6 New Comment:
Sorry, but your problem does not imply a bug in PHP itself. For a list of more appropriate places to ask for help using PHP, please visit http://www.php.net/support.php as this bug system is not the appropriate forum for asking support questions. Due to the volume of reports we can not explain in detail here why your report is not a bug. The support channels will be able to provide an explanation for you. Thank you for your interest in PHP. After the first forach $item is still a reference to the last entry, you then start to assign it again within your second loop. The following code gives the behaviour you want. <?php $list = array('one', 'two', 'three', 'four'); foreach ($list as &$item) { $item = "Row $item"; } unset($item); print_r($list); foreach ($list as $index => $item) { printf("Item #%u: %s\n", $index, $item); } var_dump($list); ?> Previous Comments: ------------------------------------------------------------------------ [2008-09-19 12:56:28] martin dot akesson at qbrick dot com Description: ------------ Using a foreach loop to edit values by reference in an array will mangle the array. From testing seems the last item in the array will be replaced by the second last item. It seems the problem shows once the array has been traversed start to end. The code to reproduce the problem does a much better job describing the issue. Reproduce code: --------------- <?php $list = array('one', 'two', 'three', 'four'); foreach ($list as &$item) { $item = "Row $item"; } print_r($list); foreach ($list as $index => $item) { printf("Item #%u: %s\n", $index, $item); } print_r($list); ?> Expected result: ---------------- Array ( [0] => Row one [1] => Row two [2] => Row three [3] => Row four ) Item #0: Row one Item #1: Row two Item #2: Row three Item #3: Row four Array ( [0] => Row one [1] => Row two [2] => Row three [3] => Row four ) Actual result: -------------- Array ( [0] => Row one [1] => Row two [2] => Row three [3] => Row four ) Item #0: Row one Item #1: Row two Item #2: Row three Item #3: Row three Array ( [0] => Row one [1] => Row two [2] => Row three [3] => Row three ) ------------------------------------------------------------------------ -- Edit this bug report at http://bugs.php.net/?id=46123&edit=1