不能一次修改一次而没有奇怪的错误

我有空闲时间,任何给定的员工现在都从他们的日历中提取到一个多维数组中,其中每个 [i][] 是一对日期,表示开始时间 [i][0] 和结束时间 [ i][1] 的空闲时间块。我需要把它分成块的开始时间和结束时间之间的 20 分钟间隔,并以该格式返回所有时间。现在我只是不断地重复修改同一个对象,但是为了正确的迭代次数,它让我发疯......希望你们能帮忙。


我已经尝试了我的 C++ 知识库提供的所有内容,但我猜 php 使用对象作为参考,我找不到解决方法。


$test = array_chunk($times, 2);

$i = 0;

$end_test = count($times)/2;

$free_slots = array();

$interval = '+20 minutes';

array_push($free_slots, $test[0][0]);

for($i=0;$i<$end_test;$i++){

    $test1 = clone $test[$i][0];

    $test2 = clone $test[$i][1];

    while($test1<=$test2){

        $test1->modify($interval);

        array_push($free_slots, $test1);

    }

}

所以在我把日期配对成对联并运行我第一次粘贴的代码是正确的之后,它就变得混乱了:


08:00:00.000000",10:20:00.000000", 10:20:00.000000", 10:20:00.000000", 10:20:00.000000", 10:20:00.000000", 10:20:00.000000", 10:20:00.000000", 12:20:00.000000", 12:20:00.000000", 12:20:00.000000",  12:20:00.000000", 16:20:00.000000", etc...

并且为空闲块存储了开始和结束时间的日期时间对象数组如下所示:


[0][0]="2019-08-14 08:00:00.000000"

[0][1]="2019-08-14 10:00:00.000000"

[1][0]="2019-08-14 11:00:00.000000"

[1][1]="2019-08-14 12:00:00.000000"

[2][0]="2019-08-14 13:00:00.000000"

[2][1]="2019-08-14 16:00:00.000000"

[3][0]="2019-08-14 17:00:00.000000"

[3][1]="2019-08-14 19:00:00.000000"


慕婉清6462132
浏览 135回答 2
2回答

忽然笑

这对我有用:&nbsp; //split into start and end points&nbsp; &nbsp; &nbsp; &nbsp; array_push($times, $last);&nbsp; &nbsp; &nbsp; &nbsp; $test = array_chunk($times, 2);&nbsp; &nbsp; &nbsp; &nbsp; $free_slots = array();&nbsp; &nbsp; &nbsp; &nbsp; $interval = '+20 minutes';&nbsp; &nbsp; &nbsp; &nbsp; $end_test = count($times)/2;&nbsp; &nbsp; &nbsp; &nbsp; //split into 'x' min intervals&nbsp; &nbsp; &nbsp; &nbsp; for($i=0; $i<$end_test; $i++){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; array_push($free_slots, $test[$i][0]);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $mod = clone $test[$i][0];&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; while($mod < $test[$i][1]){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $m = clone $mod->modify($interval);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; array_push($free_slots, $m);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if($m != $test[$i][1]){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; array_push($free_slots, $m);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }

慕妹3242003

您$test1每次都需要通过内部循环进行克隆。否则,您只是在适当的位置修改它并推送对相同对象的引用。但是,在分配$test1and时不需要克隆原始日期$test2。你永远不会修改$test2,所以它不需要是一个克隆。并且$test1在克隆它之后,您在进入循环之前不会进行修改。for($i=0;$i<$end_test;$i++){&nbsp; &nbsp; $test1 = $test[$i][0];&nbsp; &nbsp; $test2 = $test[$i][1];&nbsp; &nbsp; while($test1<=$test2){&nbsp; &nbsp; &nbsp; &nbsp; $test1 = clone $test1;&nbsp; &nbsp; &nbsp; &nbsp; $test1->modify($interval);&nbsp; &nbsp; &nbsp; &nbsp; array_push($free_slots, $test1);&nbsp; &nbsp; }}
打开App,查看更多内容
随时随地看视频慕课网APP