Can somebody explain why the second foreach loop outputs the item with ID = 2 twice?
The '&' in the first loop is intended to alter the real object and not a copy. When I change field name $value in second loop to something else, it works as expected but I would like to understand, why it comes to this output.
$a = array("ID" => "1");
$b = array("ID" => "2");
$d = array("ID" => "3");
$c = array($a, $b, $d);
foreach ($c as &$value)
{
$value['foo'] = rand(0, 10000);
}
print_r($c);
foreach ($c as $value)
{
print_r($value);
}
output:
Array
(
[0] => Array
(
[ID] => 1
[foo] => 5673
)
[1] => Array
(
[ID] => 2
[foo] => 5421
)
[2] => Array
(
[ID] => 3
[foo] => 149
)
)
Array
(
[ID] => 1
[foo] => 5673
)
Array
(
[ID] => 2
[foo] => 5421
)
Array
(
[ID] => 2
[foo] => 5421
)