我有这个功能:
public function get_free_courtrooms() {
$post = file_get_contents('php://input');
$_POST = json_decode($post, true);
$date = $this->input->post('date', true);
$time_start = $this->input->post('time_start', true);
$time_end = $this->input->post('time_end', true);
$where = array('date' => $date);
$reservations = $this->MY_Model->get('reservations', $where);
$courtrooms = $this->MY_Model->get('courtrooms');
$output = array(
'free_courtrooms' => $free_courtrooms
);
echo json_encode($output);
exit();
}
我需要从发送小时($ time_start,$ time_end)预订的$ courtroom中删除所有法庭。
在预订表中,我有:
答案 0 :(得分:3)
我将从简单的功能开始检查两次重叠:
function isOverlap($s1, $e1, $s2, $e2) {
return (($s1 > $s2 && $s1 < $e2) || ($e1 > $s2 && $e1 < $e2)
|| ($s1 < $s2 && $e1 > $2) || ($s1 > $s2 && $e1 < $e2));
}
这将是4种情况的可视化:
--- | --- | ------- | ---
--- | --- | --- | -------
现在只需填充所有繁忙房间的数组即可:
foreach($reservations as $res) {
if (isOverlap($time_start, $time_end, $res["start"], $res["end"]))
$busy_courtrooms[] = $res["id"];
}
现在使用array-diff计算所有$courtrooms
和$busy_courtrooms
之间的差异:
$free_courtrooms = array_diff($courtrooms, $busy_courtrooms);
您完成了
我现在不知道如何构建$reservations
数组,但是我想它具有“开始”和“结束”属性...