所以我的代码是:
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" } } }