#48789 [NEW]: foreach($a as $k => &$v) gives unexpected behavior

[email protected] ("ehsmeng at gmail dot com")
Newsgroups php.bugs
Message-ID <[email protected]>
From:             ehsmeng at gmail dot com
Operating system: Windows XP
PHP version:      5.2.10
PHP Bug Type:     Arrays related
Bug description:  foreach($a as $k => &$v) gives unexpected behavior

Description:
------------
The last foreach seems to corrupt $rows_unsorted. The last var_dump() in
the log reports false.

Reproduce code:
---------------
    $rows_unsorted = array();
    for ($i = 0; $i < 4; $i++)
    {
        array_push($rows_unsorted, array("apa" . $i));
    }

    $rows_index = array();
    foreach ($rows_unsorted as $k => &$row)
    {
        array_push($rows_index, sprintf("%03d_%d", strlen($row[0]), $k));
    }

    for ($i = 0; $i < 4; $i++)
    {
        var_dump($rows_unsorted[$i]);
    }

    foreach ($rows_index as $idx)
    {
        list ($len, $rowOffs) = split('_', $idx);
        $row = false;
        $row = $rows_unsorted[0 + $rowOffs];

        var_dump($row);
    }

Expected result:
----------------
I was expecting all values in $rows_unsorted to have been dumped.


-- 
Edit bug report at http://bugs.php.net/?id=48789&edit=1
-- 
Try a CVS snapshot (PHP 5.2):        http://bugs.php.net/fix.php?id=48789&r=trysnapshot52
Try a CVS snapshot (PHP 5.3):        http://bugs.php.net/fix.php?id=48789&r=trysnapshot53
Try a CVS snapshot (PHP 6.0):        http://bugs.php.net/fix.php?id=48789&r=trysnapshot60
Fixed in CVS:                        http://bugs.php.net/fix.php?id=48789&r=fixedcvs
Fixed in CVS and need be documented: http://bugs.php.net/fix.php?id=48789&r=needdocs
Fixed in release:                    http://bugs.php.net/fix.php?id=48789&r=alreadyfixed
Need backtrace:                      http://bugs.php.net/fix.php?id=48789&r=needtrace
Need Reproduce Script:               http://bugs.php.net/fix.php?id=48789&r=needscript
Try newer version:                   http://bugs.php.net/fix.php?id=48789&r=oldversion
Not developer issue:                 http://bugs.php.net/fix.php?id=48789&r=support
Expected behavior:                   http://bugs.php.net/fix.php?id=48789&r=notwrong
Not enough info:                     http://bugs.php.net/fix.php?id=48789&r=notenoughinfo
Submitted twice:                     http://bugs.php.net/fix.php?id=48789&r=submittedtwice
register_globals:                    http://bugs.php.net/fix.php?id=48789&r=globals
PHP 4 support discontinued:          http://bugs.php.net/fix.php?id=48789&r=php4
Daylight Savings:                    http://bugs.php.net/fix.php?id=48789&r=dst
IIS Stability:                       http://bugs.php.net/fix.php?id=48789&r=isapi
Install GNU Sed:                     http://bugs.php.net/fix.php?id=48789&r=gnused
Floating point limitations:          http://bugs.php.net/fix.php?id=48789&r=float
No Zend Extensions:                  http://bugs.php.net/fix.php?id=48789&r=nozend
MySQL Configuration Error:           http://bugs.php.net/fix.php?id=48789&r=mysqlcfg
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.