ID: 40654 Comment by: same at saeven dot net Reported By: saeven at saeven dot net Status: Open Bug Type: Scripting Engine problem PHP Version: 5.2.1 New Comment:
I'd add that print_r properly displays the constructed array, which contradicts what foreach displays (which is why I thought this required filing). Previous Comments: ------------------------------------------------------------------------ [2007-02-27 04:44:35] saeven at saeven dot net 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 this bug report at http://bugs.php.net/?id=40654&edit=1