|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits
[2018-05-13 16:10 UTC] peehaa@php.net
-Status: Open
+Status: Not a bug
[2018-05-13 16:10 UTC] peehaa@php.net
|
|||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Thu Nov 13 19:00:01 2025 UTC |
Description: ------------ Foreach an array by reference will change the last value (previous). Test script: --------------- <?php $array = [ ['a' => 'aa'], ['b' => 'bb'], ['c' => 'cc'], ['d' => 'dd'], ['e' => 'ee'], ['f' => 'ff'], ]; foreach ($array as & $item) { } var_dump($array); /* here will output: array(6) { [0]=> array(1) { ["a"]=> string(2) "aa" } [1]=> array(1) { ["b"]=> string(2) "bb" } [2]=> array(1) { ["c"]=> string(2) "cc" } [3]=> array(1) { ["d"]=> string(2) "dd" } [4]=> array(1) { ["e"]=> string(2) "ee" } [5]=> &array(1) { ["f"]=> string(2) "ff" } } */ echo '=====================================', PHP_EOL; foreach ($array as $item) { var_dump($item); } /* array(1) { ["a"]=> string(2) "aa" } array(1) { ["b"]=> string(2) "bb" } array(1) { ["c"]=> string(2) "cc" } array(1) { ["d"]=> string(2) "dd" } array(1) { ["e"]=> string(2) "ee" } array(1) { ["e"]=> string(2) "ee" // => this one was changed. } */