Ask a Question related to PHP Bugs, Design and Development.
-
saeven at saeven dot net #1
#40654 [NEW]: reference causes foreach to fail proper iteration
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 [url]http://bugs.php.net/?id=40654&edit=1[/url]
--
Try a CVS snapshot (PHP 4.4): [url]http://bugs.php.net/fix.php?id=40654&r=trysnapshot44[/url]
Try a CVS snapshot (PHP 5.2): [url]http://bugs.php.net/fix.php?id=40654&r=trysnapshot52[/url]
Try a CVS snapshot (PHP 6.0): [url]http://bugs.php.net/fix.php?id=40654&r=trysnapshot60[/url]
Fixed in CVS: [url]http://bugs.php.net/fix.php?id=40654&r=fixedcvs[/url]
Fixed in release: [url]http://bugs.php.net/fix.php?id=40654&r=alreadyfixed[/url]
Need backtrace: [url]http://bugs.php.net/fix.php?id=40654&r=needtrace[/url]
Need Reproduce Script: [url]http://bugs.php.net/fix.php?id=40654&r=needscript[/url]
Try newer version: [url]http://bugs.php.net/fix.php?id=40654&r=oldversion[/url]
Not developer issue: [url]http://bugs.php.net/fix.php?id=40654&r=support[/url]
Expected behavior: [url]http://bugs.php.net/fix.php?id=40654&r=notwrong[/url]
Not enough info: [url]http://bugs.php.net/fix.php?id=40654&r=notenoughinfo[/url]
Submitted twice: [url]http://bugs.php.net/fix.php?id=40654&r=submittedtwice[/url]
register_globals: [url]http://bugs.php.net/fix.php?id=40654&r=globals[/url]
PHP 3 support discontinued: [url]http://bugs.php.net/fix.php?id=40654&r=php3[/url]
Daylight Savings: [url]http://bugs.php.net/fix.php?id=40654&r=dst[/url]
IIS Stability: [url]http://bugs.php.net/fix.php?id=40654&r=isapi[/url]
Install GNU Sed: [url]http://bugs.php.net/fix.php?id=40654&r=gnused[/url]
Floating point limitations: [url]http://bugs.php.net/fix.php?id=40654&r=float[/url]
No Zend Extensions: [url]http://bugs.php.net/fix.php?id=40654&r=nozend[/url]
MySQL Configuration Error: [url]http://bugs.php.net/fix.php?id=40654&r=mysqlcfg[/url]
saeven at saeven dot net Guest
-
#35106 [Asn->Csd]: nested foreach fails when array variable has a reference
ID: 35106 Updated by: dmitry@php.net Reported By: olympnn at nm dot ru -Status: Assigned +Status: ... -
#39307 [NEW]: foreach call by reference BUG
From: lolitka at gmail dot com Operating system: Windows XP PHP version: 5.1.6 PHP Bug Type: Unknown/Other Function Bug... -
Proper way to "foreach" through a two dimensional array?
Hi folks, I seem to be getting "Illegal offset type" errors. Here is a portion of the array values: $FormVarsAry = "FirstName";... -
#8373 [Com]: foreach by reference
ID: 8373 Comment by: giles_m_allen at yahoo dot co dot uk Reported By: ocomte at guarantycity dot com Status: ... -
How to bail from an iteration but not the program?...
All- How can I terminate the running of a program from within an iteration, but not leave the program? EG, In this code: ... -
same at saeven dot net #2
#40654 [Com]: reference causes foreach to fail proper iteration
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 [url]http://bugs.php.net/?id=40654&edit=1[/url]
same at saeven dot net Guest
-
tony2001@php.net #3
#40654 [Opn->Bgs]: reference causes foreach to fail proper iteration
ID: 40654
Updated by: [email]tony2001@php.net[/email]
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 [url]http://bugs.php.net/?id=40654&edit=1[/url]
tony2001@php.net Guest



Reply With Quote

