|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits
[2016-02-09 08:58 UTC] requinix@php.net
-Status: Open
+Status: Not a bug
[2016-02-09 08:58 UTC] requinix@php.net
[2016-02-09 09:58 UTC] zjoclips at gmail dot com
|
|||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Sat Nov 08 20:00:02 2025 UTC |
Description: ------------ Hi, I opened a wrong thread last time, I didn't see the real bug. These bug happens with foreach and pointer in to declaration of this foreach. The type of last element of an array was modified from "array" into "&array" and this create the problem when i use another iteraction in this array. I'm not good to explain me but the code can do what I cannot. Ps. You can see the difference on the last element in the two var_dumps: before and after the foreach with the pointer. Test script: --------------- <?php $array = array( array("field1" => "content1.1", "field2" => "content1.2", "field3" => "content1.3", ), array( "field1" => "content2.1", "field2" => "content2.2", "field3" => "content2.3", ), array( "field1" => "content3.1", "field2" => "content3.2", "field3" => "content3.3", ), array( "field1" => "content4.1", "field2" => "content4.2", "field3" => "content4.3", ) ); echo "<pre>"; var_dump($array); echo "\n"; foreach($array as &$element){ $element["field2"] = "content modified"; } var_dump($array); echo "\n"; foreach($array as $element){ foreach($element as $cell){ echo $cell."\t"; } echo "\n"; } echo "</pre>"; Expected result: ---------------- array(4) { [0]=> array(3) { ["field1"]=> string(10) "content1.1" ["field2"]=> string(10) "content1.2" ["field3"]=> string(10) "content1.3" } [1]=> array(3) { ["field1"]=> string(10) "content2.1" ["field2"]=> string(10) "content2.2" ["field3"]=> string(10) "content2.3" } [2]=> array(3) { ["field1"]=> string(10) "content3.1" ["field2"]=> string(10) "content3.2" ["field3"]=> string(10) "content3.3" } [3]=> array(3) { ["field1"]=> string(10) "content4.1" ["field2"]=> string(10) "content4.2" ["field3"]=> string(10) "content4.3" } } array(4) { [0]=> array(3) { ["field1"]=> string(10) "content1.1" ["field2"]=> string(16) "content modified" ["field3"]=> string(10) "content1.3" } [1]=> array(3) { ["field1"]=> string(10) "content2.1" ["field2"]=> string(16) "content modified" ["field3"]=> string(10) "content2.3" } [2]=> array(3) { ["field1"]=> string(10) "content3.1" ["field2"]=> string(16) "content modified" ["field3"]=> string(10) "content3.3" } [3]=> array(3) { ["field1"]=> string(10) "content4.1" ["field2"]=> string(16) "content modified" ["field3"]=> string(10) "content4.3" } } content1.1 content modified content1.3 content2.1 content modified content2.3 content3.1 content modified content3.3 content4.1 content modified content4.3 Actual result: -------------- array(4) { [0]=> array(3) { ["field1"]=> string(10) "content1.1" ["field2"]=> string(10) "content1.2" ["field3"]=> string(10) "content1.3" } [1]=> array(3) { ["field1"]=> string(10) "content2.1" ["field2"]=> string(10) "content2.2" ["field3"]=> string(10) "content2.3" } [2]=> array(3) { ["field1"]=> string(10) "content3.1" ["field2"]=> string(10) "content3.2" ["field3"]=> string(10) "content3.3" } [3]=> array(3) { ["field1"]=> string(10) "content4.1" ["field2"]=> string(10) "content4.2" ["field3"]=> string(10) "content4.3" } } array(4) { [0]=> array(3) { ["field1"]=> string(10) "content1.1" ["field2"]=> string(16) "content modified" ["field3"]=> string(10) "content1.3" } [1]=> array(3) { ["field1"]=> string(10) "content2.1" ["field2"]=> string(16) "content modified" ["field3"]=> string(10) "content2.3" } [2]=> array(3) { ["field1"]=> string(10) "content3.1" ["field2"]=> string(16) "content modified" ["field3"]=> string(10) "content3.3" } [3]=> &array(3) { ["field1"]=> string(10) "content4.1" ["field2"]=> string(16) "content modified" ["field3"]=> string(10) "content4.3" } } content1.1 content modified content1.3 content2.1 content modified content2.3 content3.1 content modified content3.3 content3.1 content modified content3.3