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

From: Date: Tue, 09 Feb 2016 08:42:43 +0000
Subject: Bug #71557 [NEW]: Foreach and pointers with multidimensional arrays
Groups: php.bugs 
Request: Send a blank email to php-bugs+get-199120@lists.php.net to get a copy of this message
From: zjoclips at gmail dot com Operating system: Linux PHP version: Irrelevant Package: *General Issues Bug Type: Bug Bug description:Foreach and pointers with multidimensional arrays 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 bug report at https://bugs.php.net/bug.php?id=71557&edit=1 -- Try a snapshot (PHP 5.4): https://bugs.php.net/fix.php?id=71557&r=trysnapshot54 Try a snapshot (PHP 5.5): https://bugs.php.net/fix.php?id=71557&r=trysnapshot55 Try a snapshot (trunk): https://bugs.php.net/fix.php?id=71557&r=trysnapshottrunk Fixed in SVN: https://bugs.php.net/fix.php?id=71557&r=fixed Fixed in release: https://bugs.php.net/fix.php?id=71557&r=alreadyfixed Need backtrace: https://bugs.php.net/fix.php?id=71557&r=needtrace Need Reproduce Script: https://bugs.php.net/fix.php?id=71557&r=needscript Try newer version: https://bugs.php.net/fix.php?id=71557&r=oldversion Not developer issue: https://bugs.php.net/fix.php?id=71557&r=support Expected behavior: https://bugs.php.net/fix.php?id=71557&r=notwrong Not enough info: https://bugs.php.net/fix.php?id=71557&r=notenoughinfo Submitted twice: https://bugs.php.net/fix.php?id=71557&r=submittedtwice register_globals: https://bugs.php.net/fix.php?id=71557&r=globals PHP 4 support discontinued: https://bugs.php.net/fix.php?id=71557&r=php4 Daylight Savings: https://bugs.php.net/fix.php?id=71557&r=dst IIS Stability: https://bugs.php.net/fix.php?id=71557&r=isapi Install GNU Sed: https://bugs.php.net/fix.php?id=71557&r=gnused Floating point limitations: https://bugs.php.net/fix.php?id=71557&r=float No Zend Extensions: https://bugs.php.net/fix.php?id=71557&r=nozend MySQL Configuration Error: https://bugs.php.net/fix.php?id=71557&r=mysqlcfg

« previous php.bugs (#199120) next »