From:             mfuhrman at enetarch dot net
Operating system: Linux svm0907pdv 2.6.18-308.8.2.
PHP version:      5.3Git-2013-01-18 (snap)
Package:          Scripting Engine problem
Bug Type:         Bug
Bug description:ForEach fails to add new key/value pairs

Description:
------------
using PHP Version 5.3.3, Build Date Jun 25 2012 04:51:11

In testing updating nested arrays, I'm finding the that the results of an
array 
is not updated, since the variable used to point to that portion of the
array 
seems to be a copy of the data and not a direct reference to the data.

Below is an array of arrays.

In the for loop, $sub points to $aryJunk's elements .. 0 .. 4
respectively.

When $sub has a new key value pair added, ex. "munch" => "junk", this value
only 
exists as long as $sub exists in the for loop. Outside that loop, the key
value 
pair are lost.

It is my understanding that this key value pair should not be lost.  Or, at

least I cannot find an example that says definitively one way or the
other.

Please test to see if this issue still exists in the latest version of PHP,
and 
determine the next steps to resolution.

Michael J. Fuhrman
mfuhr...@enetarch.net
skype ENetArch


Test script:
---------------
$aryJunk = Array 
(
0 => array(),
1 => array(),
2 => array(),
3 => array(),
4 => array(),
);

// fails

foreach ($aryJunk AS $sub)
        $sub ["munch"] = "junk";

print_r ($aryJunk);

// works

for ($t=0; $t < count ($aryJunk); $t++)
        $aryJunk[$t] ["munch"] = "junk";
        
print_r ($aryJunk);


Expected result:
----------------
Array ( [0] => Array ( [munch] => junk ) [1] => Array ( [munch] => junk )
[2] => 
Array ( [munch] => junk ) [3] => Array ( [munch] => junk ) [4] => Array (
[munch] 
=> junk ) )

Actual result:
--------------
Array ( [0] => Array ( ) [1] => Array ( ) [2] => Array ( ) [3] => Array ( )
[4] => 
Array ( ) )

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

Reply via email to