From:             martin dot akesson at qbrick dot com
Operating system: FreeBSD 7.0
PHP version:      5.2.6
PHP Bug Type:     Arrays related
Bug description:  Problem using arrays and value references

Description:
------------
Using a foreach loop to edit values by reference in an array will mangle 
the array.  From testing seems the last item in the array will be 
replaced by the second last item.  It seems the problem shows once the 
array has been traversed start to end.

The code to reproduce the problem does a much better job describing the 
issue.

Reproduce code:
---------------
<?php

$list = array('one', 'two', 'three', 'four');

foreach ($list as &$item) {
    $item = "Row $item";
}

print_r($list);

foreach ($list as $index => $item) {
    printf("Item #%u: %s\n", $index, $item);
}

print_r($list);
?>



Expected result:
----------------
Array
(
    [0] => Row one
    [1] => Row two
    [2] => Row three
    [3] => Row four
)
Item #0: Row one
Item #1: Row two
Item #2: Row three
Item #3: Row four
Array
(
    [0] => Row one
    [1] => Row two
    [2] => Row three
    [3] => Row four
)





Actual result:
--------------
Array
(
    [0] => Row one
    [1] => Row two
    [2] => Row three
    [3] => Row four
)
Item #0: Row one
Item #1: Row two
Item #2: Row three
Item #3: Row three
Array
(
    [0] => Row one
    [1] => Row two
    [2] => Row three
    [3] => Row three
)





-- 
Edit bug report at http://bugs.php.net/?id=46123&edit=1
-- 
Try a CVS snapshot (PHP 5.2): 
http://bugs.php.net/fix.php?id=46123&r=trysnapshot52
Try a CVS snapshot (PHP 5.3): 
http://bugs.php.net/fix.php?id=46123&r=trysnapshot53
Try a CVS snapshot (PHP 6.0): 
http://bugs.php.net/fix.php?id=46123&r=trysnapshot60
Fixed in CVS:                 http://bugs.php.net/fix.php?id=46123&r=fixedcvs
Fixed in release:             
http://bugs.php.net/fix.php?id=46123&r=alreadyfixed
Need backtrace:               http://bugs.php.net/fix.php?id=46123&r=needtrace
Need Reproduce Script:        http://bugs.php.net/fix.php?id=46123&r=needscript
Try newer version:            http://bugs.php.net/fix.php?id=46123&r=oldversion
Not developer issue:          http://bugs.php.net/fix.php?id=46123&r=support
Expected behavior:            http://bugs.php.net/fix.php?id=46123&r=notwrong
Not enough info:              
http://bugs.php.net/fix.php?id=46123&r=notenoughinfo
Submitted twice:              
http://bugs.php.net/fix.php?id=46123&r=submittedtwice
register_globals:             http://bugs.php.net/fix.php?id=46123&r=globals
PHP 4 support discontinued:   http://bugs.php.net/fix.php?id=46123&r=php4
Daylight Savings:             http://bugs.php.net/fix.php?id=46123&r=dst
IIS Stability:                http://bugs.php.net/fix.php?id=46123&r=isapi
Install GNU Sed:              http://bugs.php.net/fix.php?id=46123&r=gnused
Floating point limitations:   http://bugs.php.net/fix.php?id=46123&r=float
No Zend Extensions:           http://bugs.php.net/fix.php?id=46123&r=nozend
MySQL Configuration Error:    http://bugs.php.net/fix.php?id=46123&r=mysqlcfg

Reply via email to