#40654 [Opn->Bgs]: reference causes foreach to fail proper iteration

From: Date: Tue, 27 Feb 2007 12:30:17 +0000
Subject: #40654 [Opn->Bgs]: reference causes foreach to fail proper iteration
References: 1  Groups: php.bugs 
Request: Send a blank email to php-bugs+get-109635@lists.php.net to get a copy of this message
ID: 40654 Updated by: tony2001@php.net Reported By: saeven at saeven dot net -Status: Open +Status: Bogus Bug Type: Scripting Engine problem PHP Version: 5.2.1 New Comment: Duplicate of bug #29992. Previous Comments: ------------------------------------------------------------------------ [2007-02-27 05:00:30] same at saeven dot net I'd add that print_r properly displays the constructed array, which contradicts what foreach displays (which is why I thought this required filing). ------------------------------------------------------------------------ [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

« previous php.bugs (#109635) next »