Edit report at https://bugs.php.net/bug.php?id=60534&edit=1
ID: 60534 Comment by: anon at anon dot anon Reported by: aron dot cederholm at gmail dot com Summary: Iterating an array twice w and w/o references changes last item to second last Status: Open Type: Bug Package: Arrays related Operating System: Arch Linux PHP Version: 5.3.8 Block user comment: N Private report: N New Comment: This gets submitted as a bug about twice a month. It's still not a bug. After the first loop finishes $a is a reference to the last element of the array. That means that during the second loop, the last value of the array is constantly being modified by the loop. Once it gets to the last element, 'd' has been overwritten already. foreach ($array as &$a) { echo "($a): " . join(',', $array) . "\n"; } foreach ($array as $a) { echo "($a): " . join(',', $array) . "\n"; } (a): a,b,c,d (b): a,b,c,d (c): a,b,c,d (d): a,b,c,d (a): a,b,c,a (b): a,b,c,b (c): a,b,c,c (c): a,b,c,c As it says in the manual, unset() after foreach'ing by reference: http://www.php.net/manual/en/control-structures.foreach.php Previous Comments: ------------------------------------------------------------------------ [2011-12-15 15:15:12] aron dot cederholm at gmail dot com Description: ------------ Iterating an array twice, first time with references, second time *without* references, will trigger a bug in the second iteration where the last item of the array is overwritten by the second last item in the array. This seem to be the same bug as #46123, which was dismissed on erroneous grounds Test script: --------------- <? # For a more verbose look at this bug, watch http://pastebin.com/gq2qekYh $array = array('a', 'b', 'c', 'd'); # testing array foreach ($array as &$a) {} foreach ($array as $a) {} echo join(',', $array); # will produce a,b,c,c *not* a,b,c,d as expected die(); ## while, these will work as expected # 1 foreach ($array as $a) {} print_r($array); die(); # 2 foreach ($array as &$a) {} print_r($array); die(); # 3 foreach ($array as &$a) {} foreach ($array as &$a) {} print_r($array); die(); # 4 foreach ($array as $a) {} foreach ($array as $a) {} print_r($array); die(); # 5 foreach ($array as $a) {} foreach ($array as &$a) {} print_r($array); die(); ?> Expected result: ---------------- a,b,c,d Actual result: -------------- a,b,c,c ------------------------------------------------------------------------ -- Edit this bug report at https://bugs.php.net/bug.php?id=60534&edit=1