|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
[2011-12-15 15:15 UTC] 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 PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits
|
|||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Thu Oct 30 02:00:01 2025 UTC |
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.phpAwesome quick response thanks! This is why I love php, huge community! Though I have no idea what that meant ;-p FYI for anyone having this issue just prefix your foreach with an unset: unset($value); //if $value already exists you better unset it first ..or tear your hair out! foreach ($array as $value) { // etc