0

オブジェクトを含む配列項目をコピーしたいと思います。

$arr = [
    'first_item' => [
        'label' => 'a',
        'date' => new DateTime('1999-12-30')
    ],
    'second_item' => [
        'label' => 'b',
        'date' => new DateTime('1999-12-31')
    ]
];

$copy = $arr['second_item'];
$copy['label'] = 'c'; // OK, does not affect $arr['second_item']
$copy['date']->setDate(2000, 1, 1); // changes $arr['second_item'] as well

アイデアから、置き換えようとしまし$copy = $arr['second_item']$copy = clone $arr['second_item']が、(当然のことながら) *「致命的なエラー: __clone メソッドが非オブジェクトで呼び出されました」* メッセージで失敗します。

4

0 に答える 0