错误增加了二维数组或通过二维数组循环 [英] Error in adding to 2d array or looping through 2d array

查看:151
本文介绍了错误增加了二维数组或通过二维数组循环的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在这个code的一个问题:

I have a problem in this code:

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

var_dump($allDates);

循环中的回波显示正确的值,但vardump显示最后日期要添加到阵列中的阵列的所有的位置

the echo in the loop shows the correct values but vardump shows the last dates to be added to the array in all the positions of the array

推荐答案

我不认为你需要2个循环为....

I don't think you need 2 loops for that ....

该错误是从你的循环

while ($end <= $to){
                ^-------  This was never used

另请参见

$currentDates = array("from" => $start, "to"=>$end);
         Not in the Condition  --^              ^---- To means something else 

您同时可以简单的

$start = new DateTime("2012-4-12");
$end = new DateTime("2012-12-12");
$dv = new DateInterval('P24D'); // Every 24 days

echo "<pre>";
while ( $start <= $end ) {
    echo "From ", $start->format('Y-m-d');
    $start->add($dv);
    echo " To ", $start->format('Y-m-d'), PHP_EOL;
}

输出

From 2012-04-12 To 2012-05-06
From 2012-05-06 To 2012-05-30
From 2012-05-30 To 2012-06-23
From 2012-06-23 To 2012-07-17
From 2012-07-17 To 2012-08-10
From 2012-08-10 To 2012-09-03
From 2012-09-03 To 2012-09-27
From 2012-09-27 To 2012-10-21
From 2012-10-21 To 2012-11-14
From 2012-11-14 To 2012-12-08
From 2012-12-08 To 2013-01-01

这篇关于错误增加了二维数组或通过二维数组循环的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆