Php foreach 在时隙间隔中循环,如果 between 空闲,则仅返回四分之一时间

我有一个 mySQL 表'timeslots',其中包含“intervals”和“reserved”列。我如何循环遍历它并仅返回 15 分钟的间隔,但必须保留“剪切”15 分钟的间隔 = 0。例如,我应该遍历“间隔”列并返回 08:15:00 但跳过08:00:00 因为 08:08:00 间隔被保留(列保留 = 1),我也应该跳过间隔 08:45:00 因为 08:53:00 被保留 = 1。我附上了表格的图像对于这个问题。

几天来,我一直试图为此找到解决方案,但没有成功。

http://img4.mukewang.com/62c8e41e000127e602530272.jpg

public function getTimeSlots($app)

{

$timeslots = Timeslot::where('app', '=', $app)->where('reserved', '=', 0)->orderBy('intervals', 'ASC')->get();

    $timeslotlist = array();


    foreach ($timeslots as $timeSlot) {


        $timeslotlist[] = array(

            'id' => $timeSlot['id'],

            'app' => $timeSlot['app'],

            'intervals' => $timeSlot['intervals'],

            'reserved' => $timeSlot['reserved'],

        );

    }

   return json_encode($timeslotlist);

}


FFIVE
浏览 160回答 2
2回答

慕田峪7331174

您首先需要获得保留的插槽。然后在循环中检查该槽是否有下一个保留槽。public function getTimeSlots($app){$reservedSlots = Timeslot::where('app', '=', $app)->where('reserved', '=', 1)->orderBy('intervals', 'ASC')->get();$timeslots = Timeslot::where('app', '=', $app)->where('reserved', '=', 0)->orderBy('intervals', 'ASC')->get();&nbsp; &nbsp; $timeslotlist = array();&nbsp; &nbsp; foreach ($timeslots as $timeSlot) {&nbsp; &nbsp; &nbsp; &nbsp; $haveNextReservedSlot = array_filter($reservedSlots, function($v) use($timeSlot){&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;$start_date = new DateTime($timeSlot['date'].' '.$timeSlot['intervals']);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;$since_start = $start_date->diff(new DateTime($v['date'].' '.$v['intervals']));&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;return $since_start->i < 15; //if difference less than 15 min&nbsp; &nbsp; &nbsp; });&nbsp; &nbsp; &nbsp; &nbsp; if($haveNextReservedSlot) continue;&nbsp; &nbsp; &nbsp; &nbsp; $timeslotlist[] = array(&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'id' => $timeSlot['id'],&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'app' => $timeSlot['app'],&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'intervals' => $timeSlot['intervals'],&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'reserved' => $timeSlot['reserved'],&nbsp; &nbsp; &nbsp; &nbsp; );&nbsp; &nbsp; }&nbsp; &nbsp;return json_encode($timeslotlist);}

萧十郎

我想将集合转换为数组,然后比较上一个时隙和下一个时隙会更容易,例如public function getTimeSlots($app){&nbsp; &nbsp; $timeslots = Timeslot::where('app', '=', $app)->orderBy('intervals', 'ASC')->get()->toArray();&nbsp; &nbsp; $timeslotlist = array();&nbsp; &nbsp; for($i=0;$i<count($timeslots);$i++){&nbsp; &nbsp; &nbsp; &nbsp; $current_slot=strtotime($timeslots[$i]->date." ".$timeslots[$i]->intervals);&nbsp; &nbsp; &nbsp; &nbsp; if(date('i',$current_slot) % 15===0){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if((isset($timeslots[$i-1]) && $timeslots[$i-1]->reserved) or&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; (isset($timeslots[$i+1]) && $timeslots[$i+1]->reserved)){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; else{&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $timeslotlist[]=$timeslots[$i];&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp; return json_encode($timeslotlist);}这将返回 08:30。这是你想要的吗?
打开App,查看更多内容
随时随地看视频慕课网APP