#51049 [Opn]: Last Array Element Replaced by Second Last one

From: Date: Mon, 15 Feb 2010 05:03:10 +0000
Subject: #51049 [Opn]: Last Array Element Replaced by Second Last one
References: 1  Groups: php.bugs 
Request: Send a blank email to php-bugs+get-147629@lists.php.net to get a copy of this message
ID: 51049 User updated by: timminn at gmail dot com Reported By: timminn at gmail dot com Status: Open Bug Type: Arrays related Operating System: Arch Linux 2.6.31 PHP Version: 5.3.1 New Comment: the bug occurs ONLY WHEN: MUST BE used in a STRAIGHT MANNER as ARRAY IN OBJECT, as shown in the sbmitted code, in other words, if we use $sa = $o3->a; and foreach( $sa as $k => & $v ){ ... } the bug will not occur Previous Comments: ------------------------------------------------------------------------ [2010-02-15 04:47:44] timminn at gmail dot com 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 this bug report at http://bugs.php.net/?id=51049&edit=1

« previous php.bugs (#147629) next »