[英]Remove objects from array in CodeIgniter
我有這個功能:
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)預訂的$ courtrooms中刪除所有法庭。
在預訂表中,我有:
我將從簡單的功能開始檢查重疊2次:
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"];
}
現在計算所有之間的差異$courtrooms
和$busy_courtrooms
使用數組DIFF :
$free_courtrooms = array_diff($courtrooms, $busy_courtrooms);
而你完成了
我現在不知道如何建立$reservations
數組,但我想它具有“ start”和“ end”屬性...
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.