ID: 31118
Updated by: [EMAIL PROTECTED]
Reported By: dennis at inmarket dot lviv dot ua
-Status: Open
+Status: Bogus
Bug Type: Zend Engine 2 problem
Operating System: Win
PHP Version: 5.0.2
New Comment:
Please do not submit the same bug more than once. An existing
bug report already describes this very problem. Even if you feel
that your issue is somewhat different, the resolution is likely
to be the same.
Thank you for your interest in PHP.
See #29992.
Previous Comments:
------------------------------------------------------------------------
[2004-12-16 12:26:02] dennis at inmarket dot lviv dot ua
Description:
------------
If you foreach an array with a reference (foreach $a as $x=>&$y),
modifying $y, and then without it (foreach $a as $x=>$y), on the second
time it will return incorrect results.
The sample code has two functions - each iterates over array with a
reference to value and modifies it and then they foreach that array -
first function without a reference, and the second - with a reference.
The first function fails to iterate to the end of array. However, the
print_r shows the array is ok.
Reproduce code:
---------------
function test1() {
$a = array('key1'=>'value1', 'key2'=>'value2', 'key3'=>'value3');
foreach($a as $k=>&$v) {
$v .= '0';
}
print_r($a);
foreach($a as $k=>$v) {
echo $k .'=' . $v . "\r\n";
}
}
function test2() {
$a = array('key1'=>'value1', 'key2'=>'value2', 'key3'=>'value3');
foreach($a as $k=>&$v) {
$v .= '0';
}
print_r($a);
foreach($a as $k=>&$v) {
echo $k .'=' . $v . "\r\n";
}
}
test1();
test2();
Expected result:
----------------
Array
(
[key1] => value10
[key2] => value20
[key3] => value30
)
key1=value10
key2=value20 <- all values properly changed
key3=value30
Array
(
[key1] => value10
[key2] => value20
[key3] => value30
)
key1=value10
key2=value20 <- all values properly changed
key3=value30
Actual result:
--------------
Array
(
[key1] => value10
[key2] => value20
[key3] => value30
)
key1=value10
key2=value20
key3=value20 <- SHOULD BE '30'
Array
(
[key1] => value10
[key2] => value20
[key3] => value30
)
key1=value10
key2=value20
key3=value30
------------------------------------------------------------------------
--
Edit this bug report at http://bugs.php.net/?id=31118&edit=1