#40654 [NEW]: reference causes foreach to fail proper iteration
| From: | saeven at saeven dot net | Date: | Tue, 27 Feb 2007 04:44:35 +0000 |
| Subject: | #40654 [NEW]: reference causes foreach to fail proper iteration | ||
| Groups: | php.bugs | ||
| Request: | Send a blank email to php-bugs+get-109625@lists.php.net to get a copy of this message | ||
From: saeven at saeven dot net
Operating system:
PHP version: 5.2.1
PHP Bug Type: Scripting Engine problem
Bug description: reference causes foreach to fail proper iteration
Description:
------------
Using a reference to build an array, later causes foreach to fail when
reusing the reference variable name.
The bug is solved by adding unset( $element); before block B, but this
shouldn't be necessary.
Reproduce code:
---------------
// Block A - Build the Array
$a = array( 2 => array( "value" => "a" ),
4 => array( "value" => "b" ),
6 => array( "value" => "c" ) );
$b = array( "orange", "blue", "green" );
foreach( $a as $id => &$element ){
$extra = array();
foreach( $b as $color )
array_push( $extra, $color );
$element += array( 'extra' => $extra );
}
// Block B - Iterate through
foreach( $a as $element )
echo $element["value"].' ';
Expected result:
----------------
a b c
Actual result:
--------------
a b b
--
Edit bug report at http://bugs.php.net/?id=40654&edit=1
--
Try a CVS snapshot (PHP 4.4): http://bugs.php.net/fix.php?id=40654&r=trysnapshot44
Try a CVS snapshot (PHP 5.2): http://bugs.php.net/fix.php?id=40654&r=trysnapshot52
Try a CVS snapshot (PHP 6.0): http://bugs.php.net/fix.php?id=40654&r=trysnapshot60
Fixed in CVS: http://bugs.php.net/fix.php?id=40654&r=fixedcvs
Fixed in release: http://bugs.php.net/fix.php?id=40654&r=alreadyfixed
Need backtrace: http://bugs.php.net/fix.php?id=40654&r=needtrace
Need Reproduce Script: http://bugs.php.net/fix.php?id=40654&r=needscript
Try newer version: http://bugs.php.net/fix.php?id=40654&r=oldversion
Not developer issue: http://bugs.php.net/fix.php?id=40654&r=support
Expected behavior: http://bugs.php.net/fix.php?id=40654&r=notwrong
Not enough info: http://bugs.php.net/fix.php?id=40654&r=notenoughinfo
Submitted twice: http://bugs.php.net/fix.php?id=40654&r=submittedtwice
register_globals: http://bugs.php.net/fix.php?id=40654&r=globals
PHP 3 support discontinued: http://bugs.php.net/fix.php?id=40654&r=php3
Daylight Savings: http://bugs.php.net/fix.php?id=40654&r=dst
IIS Stability: http://bugs.php.net/fix.php?id=40654&r=isapi
Install GNU Sed: http://bugs.php.net/fix.php?id=40654&r=gnused
Floating point limitations: http://bugs.php.net/fix.php?id=40654&r=float
No Zend Extensions: http://bugs.php.net/fix.php?id=40654&r=nozend
MySQL Configuration Error: http://bugs.php.net/fix.php?id=40654&r=mysqlcfg