From:             dennis at inmarket dot lviv dot ua
Operating system: Win
PHP version:      5.0.2
PHP Bug Type:     *General Issues
Bug description:  Foreach same array with a reference ($x=>&$y) bug

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 bug report at http://bugs.php.net/?id=31118&edit=1
-- 
Try a CVS snapshot (php4):   http://bugs.php.net/fix.php?id=31118&r=trysnapshot4
Try a CVS snapshot (php5.0): 
http://bugs.php.net/fix.php?id=31118&r=trysnapshot50
Try a CVS snapshot (php5.1): 
http://bugs.php.net/fix.php?id=31118&r=trysnapshot51
Fixed in CVS:                http://bugs.php.net/fix.php?id=31118&r=fixedcvs
Fixed in release:            http://bugs.php.net/fix.php?id=31118&r=alreadyfixed
Need backtrace:              http://bugs.php.net/fix.php?id=31118&r=needtrace
Need Reproduce Script:       http://bugs.php.net/fix.php?id=31118&r=needscript
Try newer version:           http://bugs.php.net/fix.php?id=31118&r=oldversion
Not developer issue:         http://bugs.php.net/fix.php?id=31118&r=support
Expected behavior:           http://bugs.php.net/fix.php?id=31118&r=notwrong
Not enough info:             
http://bugs.php.net/fix.php?id=31118&r=notenoughinfo
Submitted twice:             
http://bugs.php.net/fix.php?id=31118&r=submittedtwice
register_globals:            http://bugs.php.net/fix.php?id=31118&r=globals
PHP 3 support discontinued:  http://bugs.php.net/fix.php?id=31118&r=php3
Daylight Savings:            http://bugs.php.net/fix.php?id=31118&r=dst
IIS Stability:               http://bugs.php.net/fix.php?id=31118&r=isapi
Install GNU Sed:             http://bugs.php.net/fix.php?id=31118&r=gnused
Floating point limitations:  http://bugs.php.net/fix.php?id=31118&r=float
MySQL Configuration Error:   http://bugs.php.net/fix.php?id=31118&r=mysqlcfg

Reply via email to