#48404 [NEW]: Passing variables byRef to foreach and doing array_reverse yelds unexp. results

From: Date: Wed, 27 May 2009 10:29:46 +0000
Subject: #48404 [NEW]: Passing variables byRef to foreach and doing array_reverse yelds unexp. results
Groups: php.bugs 
Request: Send a blank email to php-bugs+get-137773@lists.php.net to get a copy of this message
From: praxodas at gmail dot com Operating system: linux PHP version: 5.2.9 PHP Bug Type: Scripting Engine problem Bug description: Passing variables byRef to foreach and doing array_reverse yelds unexp. results Description: ------------ After reversing an array and passing it by reference in foreach and then doing array_reverse on itself, passing the same array by value in another foreach overwrites previous array elements with the last one. If a copy of an array is made, the copy is overwritten as well. A workaround would be to pass the array by reference in the next foreach loop. Reproduce code: --------------- <?php $demo_array=array(array('val1'=>'apple', 'ord_num'=>2),array('val2'=>'orange', 'ord_num'=>3)); $demo_array=array_reverse($demo_array); foreach ($demo_array as $key=>&$el) { $el['ord_num']=$key; } $demo_array=array_reverse($demo_array); $new_array=$demo_array; foreach ($demo_array as $k=>$el) { // do nothing,.. } echo "\r\nnew_array:\r\n"; print_r($new_array); echo "\r\ndemo_array:\r\n"; print_r($demo_array); ?> Expected result: ---------------- new_array: Array ( [0] => Array ( [val1] => apple [ord_num] => 1 ) [1] => Array ( [val2] => orange [ord_num] => 0 ) ) demo_array: Array ( [0] => Array ( [val1] => apple [ord_num] => 1 ) [1] => Array ( [val2] => orange [ord_num] => 0 ) ) Actual result: -------------- new_array: Array ( [0] => Array ( [val2] => orange [ord_num] => 0 ) [1] => Array ( [val2] => orange [ord_num] => 0 ) ) demo_array: Array ( [0] => Array ( [val2] => orange [ord_num] => 0 ) [1] => Array ( [val2] => orange [ord_num] => 0 ) ) -- Edit bug report at http://bugs.php.net/?id=48404&edit=1 -- Try a CVS snapshot (PHP 5.2): http://bugs.php.net/fix.php?id=48404&r=trysnapshot52 Try a CVS snapshot (PHP 5.3): http://bugs.php.net/fix.php?id=48404&r=trysnapshot53 Try a CVS snapshot (PHP 6.0): http://bugs.php.net/fix.php?id=48404&r=trysnapshot60 Fixed in CVS: http://bugs.php.net/fix.php?id=48404&r=fixedcvs Fixed in CVS and need be documented: http://bugs.php.net/fix.php?id=48404&r=needdocs Fixed in release: http://bugs.php.net/fix.php?id=48404&r=alreadyfixed Need backtrace: http://bugs.php.net/fix.php?id=48404&r=needtrace Need Reproduce Script: http://bugs.php.net/fix.php?id=48404&r=needscript Try newer version: http://bugs.php.net/fix.php?id=48404&r=oldversion Not developer issue: http://bugs.php.net/fix.php?id=48404&r=support Expected behavior: http://bugs.php.net/fix.php?id=48404&r=notwrong Not enough info: http://bugs.php.net/fix.php?id=48404&r=notenoughinfo Submitted twice: http://bugs.php.net/fix.php?id=48404&r=submittedtwice register_globals: http://bugs.php.net/fix.php?id=48404&r=globals PHP 4 support discontinued: http://bugs.php.net/fix.php?id=48404&r=php4 Daylight Savings: http://bugs.php.net/fix.php?id=48404&r=dst IIS Stability: http://bugs.php.net/fix.php?id=48404&r=isapi Install GNU Sed: http://bugs.php.net/fix.php?id=48404&r=gnused Floating point limitations: http://bugs.php.net/fix.php?id=48404&r=float No Zend Extensions: http://bugs.php.net/fix.php?id=48404&r=nozend MySQL Configuration Error: http://bugs.php.net/fix.php?id=48404&r=mysqlcfg

« previous php.bugs (#137773) next »