Bug #64018 [Opn]: missing elements from the result set when using foreach with referencing $value

2013-01-18 Thread potyo dot ada at gmail dot com
Edit report at https://bugs.php.net/bug.php?id=64018&edit=1

 ID: 64018
 User updated by:potyo dot ada at gmail dot com
 Reported by:potyo dot ada at gmail dot com
 Summary:missing elements from the result set when using
 foreach with referencing $value
 Status: Open
 Type:   Bug
 Package:Scripting Engine problem
 Operating System:   Ubuntu 12.10
 PHP Version:Irrelevant
 Block user comment: N
 Private report: N

 New Comment:

Tested using 5.3.5 and 5.4.10 versions


Previous Comments:

[2013-01-18 11:40:40] potyo dot ada at gmail dot com

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 this bug report at https://bugs.php.net/bug.php?id=64018&edit=1


[PHP-BUG] Bug #64018 [NEW]: missing elements from the result set when using foreach with referencing $value

2013-01-18 Thread potyo dot ada at gmail dot com
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