Bug #71557 [Nab]: Foreach and pointers with multidimensional arrays

From: Date: Tue, 09 Feb 2016 09:58:30 +0000
Subject: Bug #71557 [Nab]: Foreach and pointers with multidimensional arrays
References: 1  Groups: php.bugs 
Request: Send a blank email to php-bugs+get-199124@lists.php.net to get a copy of this message
Edit report at https://bugs.php.net/bug.php?id=71557&edit=1 ID: 71557 User updated by: zjoclips at gmail dot com Reported by: zjoclips at gmail dot com Summary: Foreach and pointers with multidimensional arrays Status: Not a bug Type: Bug Package: *General Issues Operating System: Linux PHP Version: Irrelevant Block user comment: N Private report: N New Comment: Thanks, I was in fault again. Anyway good job with this posts Previous Comments: ------------------------------------------------------------------------ [2016-02-09 08:58:13] requinix@php.net See bug #71458 ------------------------------------------------------------------------ [2016-02-09 08:42:40] zjoclips at gmail dot com 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 ------------------------------------------------------------------------ -- Edit this bug report at https://bugs.php.net/bug.php?id=71557&edit=1

« previous php.bugs (#199124) next »