PHP对象数组不包含正确的值

时间:2018-08-30 11:05:54

标签: php arrays

这是一个代码段:

$r = [];
$currencies = ['EUR','USD'];
$items = [];
$items[]    = (object)['product_name' => 'product1'];
$items[]    = (object)['product_name' => 'product2'];
$items[]    = (object)['product_name' => 'product3'];

$i = 0;
foreach($currencies as $currency)
{
    foreach($items as $key => $item)
    {
        $items[$key]->field1 = $i++;
        $items[$key]->field2 = $i++;
    }
    print_r($items);
    $r[]    = $items;
}

print_r($r);die('end');

foreach循环中的前两个print_r在字段中显示具有自动增量值的正确数组。

    Array
(
    [0] => stdClass Object
        (
            [product_name] => product1
            [field1] => 0
            [field2] => 1
        )

    [1] => stdClass Object
        (
            [product_name] => product2
            [field1] => 2
            [field2] => 3
        )

    [2] => stdClass Object
        (
            [product_name] => product3
            [field1] => 4
            [field2] => 5
        )

)
Array
(
    [0] => stdClass Object
        (
            [product_name] => product1
            [field1] => 6
            [field2] => 7
        )

    [1] => stdClass Object
        (
            [product_name] => product2
            [field1] => 8
            [field2] => 9
        )

    [2] => stdClass Object
        (
            [product_name] => product3
            [field1] => 10
            [field2] => 11
        )

)

但是最后一行的最后一个显示了一个数组,其中两个元素相同,$ r [0]包含与$ r [1]相同的值

    Array
(
    [0] => Array
        (
            [0] => stdClass Object
                (
                    [product_name] => product1
                    [field1] => 6
                    [field2] => 7
                )

            [1] => stdClass Object
                (
                    [product_name] => product2
                    [field1] => 8
                    [field2] => 9
                )

            [2] => stdClass Object
                (
                    [product_name] => product3
                    [field1] => 10
                    [field2] => 11
                )

        )

    [1] => Array
        (
            [0] => stdClass Object
                (
                    [product_name] => product1
                    [field1] => 6
                    [field2] => 7
                )

            [1] => stdClass Object
                (
                    [product_name] => product2
                    [field1] => 8
                    [field2] => 9
                )

            [2] => stdClass Object
                (
                    [product_name] => product3
                    [field1] => 10
                    [field2] => 11
                )

        )

)

对此有何猜想?

1 个答案:

答案 0 :(得分:4)

每个$items[$key]是一个对象,而php可以通过引用与对象一起使用。

这意味着,在您的任何子数组中,$items[$key]指向同一对象。而且,如果您更改一个对象(在第二迭代中)的值,则该值会反映所有其他对象(在第一次迭代中创建的对象)。如果要使用不同的对象-最好clone使用它们:

$r = [];
$currencies = ['EUR','USD'];
$items = [];
$items[]    = (object)['product_name' => 'product1'];
$items[]    = (object)['product_name' => 'product2'];
$items[]    = (object)['product_name' => 'product3'];
$i = 0;
foreach($currencies as $currency)
{
    $cloned_items = [];
    foreach($items as $key => $item)
    {
        // `clone` item so to detach it from original item
        $cloned_item = clone $item;   
        $cloned_item->field1 = $i++;
        $cloned_item->field2 = $i++;
        $cloned_items[] = $cloned_item;
    }
    print_r($cloned_items);
    $r[]    = $cloned_items;
}

print_r($r);die('end');

有关objects and references的更多信息。