Lumpy Lumpy - 6 months ago 9
PHP Question

Strange foreach loop behaviour in PHP

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
)

Answer

From one of the SO answer

This is well-documented PHP behaviour See the warning on the foreach page of php.net

Warning

Reference of a $value and the last array element remain even after the foreach loop. It is recommended to destroy it by unset().

So your code should be:

<?php
$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);

unset($value);

foreach ($c as $value)
{
    print_r($value);
}

Output:

Array
(
    [0] => Array
        (
            [ID] => 1
            [foo] => 6465
        )

    [1] => Array
        (
            [ID] => 2
            [foo] => 4562
        )

    [2] => Array
        (
            [ID] => 3
            [foo] => 2629
        )

)
Array
(
    [ID] => 1
    [foo] => 6465
)
Array
(
    [ID] => 2
    [foo] => 4562
)
Array
(
    [ID] => 3
    [foo] => 2629
)