#51049 [NEW]: Last Array Element Replaced by Second Last one
| From: | timminn at gmail dot com | Date: | Mon, 15 Feb 2010 04:47:44 +0000 |
| Subject: | #51049 [NEW]: Last Array Element Replaced by Second Last one | ||
| Groups: | php.bugs | ||
| Request: | Send a blank email to php-bugs+get-147628@lists.php.net to get a copy of this message | ||
From: timminn at gmail dot com
Operating system: Arch Linux 2.6.31
PHP version: 5.3.1
PHP Bug Type: Arrays related
Bug description: Last Array Element Replaced by Second Last one
Description:
------------
if AN ARRAY WITHIN AN OBJECT is used in "a straight manner" in the
foreach() loop, with the references to its values[poor English, see the
SECOND foreach() in the submitted code to understand I mean],
the last value in the array will be REPACED with the second last one
ACCIDENTALLY, WITHOUT ANY ASSIGNMENT OPERATION.
Reproduce code:
---------------
<?php
class myc{ public $a; }
$o3->a = Array( 11, 22 );
//loop ONE
foreach( $o3->a as $k => $v )
echo $v. ' '; //EXPECT 11 22 , CORRECT OUTPUT
echo "\n=========\n";
//loop TWO notice the reference & sign for $v
foreach( $o3->a as $k => & $v )
echo $v. ' '; //EXPECT 11 22 , CORRECT OUTPUT
echo "\n=========\n";
//loop THREE
foreach( $o3->a as $k => $v )
echo $v. ' '; //EXPECT 11 22 , BUT WRONG OUTPUT: 11 11
echo "\n=========\n";
?>
Expected result:
----------------
11 22
11 22
11 22
Actual result:
--------------
11 22
11 22
11 11
--
Edit bug report at http://bugs.php.net/?id=51049&edit=1
--
Try a snapshot (PHP 5.2): http://bugs.php.net/fix.php?id=51049&r=trysnapshot52
Try a snapshot (PHP 5.3): http://bugs.php.net/fix.php?id=51049&r=trysnapshot53
Try a snapshot (PHP 6.0): http://bugs.php.net/fix.php?id=51049&r=trysnapshot60
Fixed in SVN: http://bugs.php.net/fix.php?id=51049&r=fixed
Fixed in SVN and need be documented: http://bugs.php.net/fix.php?id=51049&r=needdocs
Fixed in release: http://bugs.php.net/fix.php?id=51049&r=alreadyfixed
Need backtrace: http://bugs.php.net/fix.php?id=51049&r=needtrace
Need Reproduce Script: http://bugs.php.net/fix.php?id=51049&r=needscript
Try newer version: http://bugs.php.net/fix.php?id=51049&r=oldversion
Not developer issue: http://bugs.php.net/fix.php?id=51049&r=support
Expected behavior: http://bugs.php.net/fix.php?id=51049&r=notwrong
Not enough info: http://bugs.php.net/fix.php?id=51049&r=notenoughinfo
Submitted twice: http://bugs.php.net/fix.php?id=51049&r=submittedtwice
register_globals: http://bugs.php.net/fix.php?id=51049&r=globals
PHP 4 support discontinued: http://bugs.php.net/fix.php?id=51049&r=php4
Daylight Savings: http://bugs.php.net/fix.php?id=51049&r=dst
IIS Stability: http://bugs.php.net/fix.php?id=51049&r=isapi
Install GNU Sed: http://bugs.php.net/fix.php?id=51049&r=gnused
Floating point limitations: http://bugs.php.net/fix.php?id=51049&r=float
No Zend Extensions: http://bugs.php.net/fix.php?id=51049&r=nozend
MySQL Configuration Error: http://bugs.php.net/fix.php?id=51049&r=mysqlcfg