php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #64027 ForEach fails to add new key/value pairs
Submitted: 2013-01-18 20:13 UTC Modified: 2013-01-19 12:34 UTC
From: mfuhrman at enetarch dot net Assigned:
Status: Not a bug Package: Scripting Engine problem
PHP Version: 5.3Git-2013-01-18 (snap) OS: Linux svm0907pdv 2.6.18-308.8.2.
Private report: No CVE-ID: None
Welcome back! If you're the original bug submitter, here's where you can edit the bug or add additional notes.
If you forgot your password, you can retrieve your password here.
Password:
Status:
Package:
Bug Type:
Summary:
From: mfuhrman at enetarch dot net
New email:
PHP Version: OS:

 

 [2013-01-18 20:13 UTC] mfuhrman at enetarch dot net
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
mfuhrman@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 ( ) )

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2013-01-18 22:00 UTC] matthew dot vince at gmail dot com
This is expected behavior. To modify values using a foreach loop, you need to 
assign by reference, as explained in the foreach documentation:

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


http://php.net/manual/en/control-structures.foreach.php
 [2013-01-19 12:34 UTC] cataphract@php.net
-Status: Open +Status: Not a bug
 [2013-01-19 12:34 UTC] cataphract@php.net
Sorry, but your problem does not imply a bug in PHP itself.  For a
list of more appropriate places to ask for help using PHP, please
visit http://www.php.net/support.php as this bug system is not the
appropriate forum for asking support questions.  Due to the volume
of reports we can not explain in detail here why your report is not
a bug.  The support channels will be able to provide an explanation
for you.

Thank you for your interest in PHP.


 
PHP Copyright © 2001-2025 The PHP Group
All rights reserved.
Last updated: Fri Jul 04 17:01:35 2025 UTC