0

重複の可能性:
DateTimeオブジェクトをディープコピーするにはどうすればよいですか?
2D配列への追加または2D配列のループでエラーが発生しました

だから私のコードは:

    while ($end <= $to){
        $currentDates = array("from" => $start, "to"=>$end);
        $allDates[] = $currentDates;
        echo '<br>', var_dump($allDates);
        unset($currentDates);
        $start->add($intervalObj);
        $end->add($intervalObj);
    }

しかし、$currentDatesが$allDatesに追加されるたびに、私が期待するように$ allDatesに位置が追加されますが、以前のすべての配列位置が$currentDatesの現在の値で上書きされます。

これは、ループ内のvar_dumpの結果です。

array(1) { [0]=> array(2) { ["from"]=> object(DateTime)#6 (3) { ["date"]=> string(19) "2012-10-10 00:00:00" ["timezone_type"]=> int(3) ["timezone"]=> string(16) "America/New_York" } ["to"]=> object(DateTime)#7 (3) { ["date"]=> string(19) "2012-10-11 00:00:00" ["timezone_type"]=> int(3) ["timezone"]=> string(16) "America/New_York" } } } 

array(2) { [0]=> array(2) { ["from"]=> object(DateTime)#6 (3) { ["date"]=> string(19) "2012-10-11 00:00:00" ["timezone_type"]=> int(3) ["timezone"]=> string(16) "America/New_York" } ["to"]=> object(DateTime)#7 (3) { ["date"]=> string(19) "2012-10-12 00:00:00" ["timezone_type"]=> int(3) ["timezone"]=> string(16) "America/New_York" } } [1]=> array(2) { ["from"]=> object(DateTime)#6 (3) { ["date"]=> string(19) "2012-10-11 00:00:00" ["timezone_type"]=> int(3) ["timezone"]=> string(16) "America/New_York" } ["to"]=> object(DateTime)#7 (3) { ["date"]=> string(19) "2012-10-12 00:00:00" ["timezone_type"]=> int(3) ["timezone"]=> string(16) "America/New_York" } } } 

array(3) { [0]=> array(2) { ["from"]=> object(DateTime)#6 (3) { ["date"]=> string(19) "2012-10-12 00:00:00" ["timezone_type"]=> int(3) ["timezone"]=> string(16) "America/New_York" } ["to"]=> object(DateTime)#7 (3) { ["date"]=> string(19) "2012-10-13 00:00:00" ["timezone_type"]=> int(3) ["timezone"]=> string(16) "America/New_York" } } [1]=> array(2) { ["from"]=> object(DateTime)#6 (3) { ["date"]=> string(19) "2012-10-12 00:00:00" ["timezone_type"]=> int(3) ["timezone"]=> string(16) "America/New_York" } ["to"]=> object(DateTime)#7 (3) { ["date"]=> string(19) "2012-10-13 00:00:00" ["timezone_type"]=> int(3) ["timezone"]=> string(16) "America/New_York" } } [2]=> array(2) { ["from"]=> object(DateTime)#6 (3) { ["date"]=> string(19) "2012-10-12 00:00:00" ["timezone_type"]=> int(3) ["timezone"]=> string(16) "America/New_York" } ["to"]=> object(DateTime)#7 (3) { ["date"]=> string(19) "2012-10-13 00:00:00" ["timezone_type"]=> int(3) ["timezone"]=> string(16) "America/New_York" } } } 
4

1 に答える 1

1

$startおよび$endはオブジェクトであり、常に参照によって割り当てられます。新しい別個のオブジェクトを作成する必要があります。DateTimeオブジェクトをディープコピーするにはどうすればよいですか?を参照してください。これを行う方法について。

于 2012-10-15T18:13:47.967 に答える