From:
Operating system: Ubuntu 11.04
PHP version: 5.3.5
Package: *General Issues
Bug Type: Bug
Bug description:Iterating twice over an array changes the array
Description:
------------
When using foreach to iterate over an array using the reference (&$value)
syntax, and using foreach a second time using the same variable name
changes the original array by overwriting the array's last item with the
second-last.
This is obviously wrong...
Hint: note the missing object's id #4 in the actual result
Test script:
---------------
// the example uses objects to show the object's id in the output, but this
error also occurs works with scalars
class A {}
$c = array(new A(), new A(), new A(), new A());
$arr = $c; // to keep the reference
var_dump($arr);
foreach ($arr as &$value) {}
var_dump($arr);
// using the same variable name
foreach ($arr as $value) {}
var_dump($arr);
Expected result:
----------------
array(4) {
[0]=>
object(A)#1 (0) {
}
[1]=>
object(A)#2 (0) {
}
[2]=>
object(A)#3 (0) {
}
[3]=>
object(A)#4 (0) {
}
}
array(4) {
[0]=>
object(A)#1 (0) {
}
[1]=>
object(A)#2 (0) {
}
[2]=>
object(A)#3 (0) {
}
[3]=>
object(A)#4 (0) {
}
}
array(4) {
[0]=>
object(A)#1 (0) {
}
[1]=>
object(A)#2 (0) {
}
[2]=>
object(A)#3 (0) {
}
[3]=>
object(A)#4 (0) {
}
}
Actual result:
--------------
array(4) {
[0]=>
object(A)#1 (0) {
}
[1]=>
object(A)#2 (0) {
}
[2]=>
object(A)#3 (0) {
}
[3]=>
object(A)#4 (0) {
}
}
array(4) {
[0]=>
object(A)#1 (0) {
}
[1]=>
object(A)#2 (0) {
}
[2]=>
object(A)#3 (0) {
}
[3]=>
&object(A)#4 (0) {
}
}
array(4) {
[0]=>
object(A)#1 (0) {
}
[1]=>
object(A)#2 (0) {
}
[2]=>
object(A)#3 (0) {
}
[3]=>
&object(A)#3 (0) {
}
}
--
Edit bug report at http://bugs.php.net/bug.php?id=53780&edit=1
--
Try a snapshot (PHP 5.2):
http://bugs.php.net/fix.php?id=53780&r=trysnapshot52
Try a snapshot (PHP 5.3):
http://bugs.php.net/fix.php?id=53780&r=trysnapshot53
Try a snapshot (trunk):
http://bugs.php.net/fix.php?id=53780&r=trysnapshottrunk
Fixed in SVN:
http://bugs.php.net/fix.php?id=53780&r=fixed
Fixed in SVN and need be documented:
http://bugs.php.net/fix.php?id=53780&r=needdocs
Fixed in release:
http://bugs.php.net/fix.php?id=53780&r=alreadyfixed
Need backtrace:
http://bugs.php.net/fix.php?id=53780&r=needtrace
Need Reproduce Script:
http://bugs.php.net/fix.php?id=53780&r=needscript
Try newer version:
http://bugs.php.net/fix.php?id=53780&r=oldversion
Not developer issue:
http://bugs.php.net/fix.php?id=53780&r=support
Expected behavior:
http://bugs.php.net/fix.php?id=53780&r=notwrong
Not enough info:
http://bugs.php.net/fix.php?id=53780&r=notenoughinfo
Submitted twice:
http://bugs.php.net/fix.php?id=53780&r=submittedtwice
register_globals:
http://bugs.php.net/fix.php?id=53780&r=globals
PHP 4 support discontinued: http://bugs.php.net/fix.php?id=53780&r=php4
Daylight Savings: http://bugs.php.net/fix.php?id=53780&r=dst
IIS Stability:
http://bugs.php.net/fix.php?id=53780&r=isapi
Install GNU Sed:
http://bugs.php.net/fix.php?id=53780&r=gnused
Floating point limitations:
http://bugs.php.net/fix.php?id=53780&r=float
No Zend Extensions:
http://bugs.php.net/fix.php?id=53780&r=nozend
MySQL Configuration Error:
http://bugs.php.net/fix.php?id=53780&r=mysqlcfg