From:             potyo dot ada at gmail dot com
Operating system: Ubuntu 12.10
PHP version:      Irrelevant
Package:          Scripting Engine problem
Bug Type:         Bug
Bug description:missing elements from the result set when using foreach with 
referencing $value

Description:
------------
If we try to change the array elements in one foreach loop using referenced

$value, and in a second foreach loop remove some elements from the array,
then 
the resulting array misses some element, some elements are duplicated, or
simply 
nothing is removed, depending on that, which element we want to remove from
the 
array in the second loop.

Test script:
---------------
$a=array(array('a'=>'11', 'aa'=>'22'), array('a'=>'33', 'aa'=>'44'),
array('a'=>'55', 'aa'=>'66'));
foreach ($a as &$v) // if we use $k=>$v here
{
        $v['c']='99'; // and $a[$k]['c']='99' here, then it produces the 
expected
result
}
foreach ($a as $k=>$v)
{
        if ($v['aa']=='22')
        {
                unset($a[$k]); // 
        }
}
print_r($a);


Expected result:
----------------
Array
(
    [1] => Array
        (
            [a] => 33
            [aa] => 44
            [c] => 99
        )

    [2] => Array
        (
            [a] => 33
            [aa] => 44
            [c] => 99
        )

)

Actual result:
--------------
Array
(
    [1] => Array
        (
            [a] => 33
            [aa] => 44
            [c] => 99
        )

    [2] => Array
        (
            [a] => 55
            [aa] => 66
            [c] => 99
        )

)


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

Reply via email to