|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits
[2016-10-06 21:12 UTC] nikic@php.net
-Status: Open
+Status: Not a bug
[2016-10-06 21:12 UTC] nikic@php.net
[2016-10-06 21:23 UTC] randy at larsongroup dot org
|
|||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Tue Nov 18 15:00:01 2025 UTC |
Description: ------------ When you loop through an array with foreach and set the value as a pointer to the array value then the last element in the array will be set as a pointer to the second to last element of the array. Then if you loop through the array a second time you will see that the last value isn't there, but points to the second to last value. Test script: --------------- <?php $originalArray = array('Thor', 'The Hulk', 'Iron Man'); var_dump($originalArray); foreach ($originalArray as &$value) { echo $value."\n"; } echo "\nSecond time through loop\n"; foreach ($originalArray as $value) { echo $value."\n"; } // notice the last element is a pointer which it shouldn't be. echo "\n".var_dump($originalArray); Expected result: ---------------- array(3) { [0]=> string(4) "Thor" [1]=> string(8) "The Hulk" [2]=> string(8) "Iron Man" } Thor The Hulk Iron Man Second time through loop Thor The Hulk Iron Man array(3) { [0]=> string(4) "Thor" [1]=> string(8) "The Hulk" [2]=> string(8) "Iron Man" } Actual result: -------------- array(3) { [0]=> string(4) "Thor" [1]=> string(8) "The Hulk" [2]=> string(8) "Iron Man" } Thor The Hulk Iron Man Second time through loop Thor The Hulk The Hulk array(3) { [0]=> string(4) "Thor" [1]=> string(8) "The Hulk" [2]=> &string(8) "The Hulk" }