#42828 [NEW]: array internal pointer in foreach

From: Date: Tue, 02 Oct 2007 11:40:31 +0000
Subject: #42828 [NEW]: array internal pointer in foreach
Groups: php.bugs 
Request: Send a blank email to php-bugs+get-118080@lists.php.net to get a copy of this message
From: ger dot kirill at gmail dot com Operating system: Windows, Linux PHP version: 5.2.4 PHP Bug Type: Unknown/Other Function Bug description: array internal pointer in foreach Description: ------------ In this php version the internal array pointer after foreach is not always NULL even after the full cicle is made. Instead it points to the next key after the one which was current when the first change was made to the array we are currently iterating. Reproduce code: --------------- $ar = array('val1', 'val2', 'val3'); var_dump(key($ar)); echo '<br>'; foreach ($ar as $key=>$val) { echo $key.'=>'.$val.'<br>'; } var_dump(key($ar)); echo '-------'; foreach ($ar as $key=>$val) { echo $key.'=>'.$val.'<br>'; $ar[2] = $ar[2]; } var_dump(key($ar)); echo '-------'; foreach ($ar as $key=>$val) { echo $key.'=>'.$val.'<br>'; if ($key==1) $ar[2] = $ar[2]; } var_dump(key($ar)); Expected result: ---------------- int(0) 0=>val1 1=>val2 2=>val3 NULL ------- 0=>val1 1=>val2 2=>val3 NULL ------- 0=>val1 1=>val2 2=>val3 NULL Actual result: -------------- int(0) 0=>val1 1=>val2 2=>val3 NULL ------- 0=>val1 1=>val2 2=>val3 int(1) ------- 0=>val1 1=>val2 2=>val3 int(2) -- Edit bug report at http://bugs.php.net/?id=42828&edit=1 -- Try a CVS snapshot (PHP 4.4): http://bugs.php.net/fix.php?id=42828&r=trysnapshot44 Try a CVS snapshot (PHP 5.2): http://bugs.php.net/fix.php?id=42828&r=trysnapshot52 Try a CVS snapshot (PHP 6.0): http://bugs.php.net/fix.php?id=42828&r=trysnapshot60 Fixed in CVS: http://bugs.php.net/fix.php?id=42828&r=fixedcvs Fixed in release: http://bugs.php.net/fix.php?id=42828&r=alreadyfixed Need backtrace: http://bugs.php.net/fix.php?id=42828&r=needtrace Need Reproduce Script: http://bugs.php.net/fix.php?id=42828&r=needscript Try newer version: http://bugs.php.net/fix.php?id=42828&r=oldversion Not developer issue: http://bugs.php.net/fix.php?id=42828&r=support Expected behavior: http://bugs.php.net/fix.php?id=42828&r=notwrong Not enough info: http://bugs.php.net/fix.php?id=42828&r=notenoughinfo Submitted twice: http://bugs.php.net/fix.php?id=42828&r=submittedtwice register_globals: http://bugs.php.net/fix.php?id=42828&r=globals PHP 3 support discontinued: http://bugs.php.net/fix.php?id=42828&r=php3 Daylight Savings: http://bugs.php.net/fix.php?id=42828&r=dst IIS Stability: http://bugs.php.net/fix.php?id=42828&r=isapi Install GNU Sed: http://bugs.php.net/fix.php?id=42828&r=gnused Floating point limitations: http://bugs.php.net/fix.php?id=42828&r=float No Zend Extensions: http://bugs.php.net/fix.php?id=42828&r=nozend MySQL Configuration Error: http://bugs.php.net/fix.php?id=42828&r=mysqlcfg

« previous php.bugs (#118080) next »