php - 从 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();
}
我需要从 $courtrooms 中删除所有在发送时间($time_start、$time_end)中预订的法庭。
在预订表中,我有:
- ID
- 法庭编号
- 日期
- 时间开始
- 时间结束
解决方案
我将从简单的函数开始检查重叠 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
使用array-diff之间的差异:
$free_courtrooms = array_diff($courtrooms, $busy_courtrooms);
你完成了
我现在不知道你的$reservations
数组是如何构建的,但我猜它有“开始”和“结束”属性......
推荐阅读
- keycloak - Keycloak OIDC 和 AWS ALB
- php - 需要使用 php 和 html 在缩略图列表中创建一个按钮
- swift - 如何以特定方式从 Swift 中的字符串中提取数字
- html - 如何用不同的颜色填充这个 svg 的另一部分,使其与我的设计融为一体?
- r - R Markdown:带有链接的响应式图像
- vue.js - 在vue中选择buefy表内的字段
- python - 您可以在不提供 Python 代码中的纯凭据的情况下连接到 MySQL 数据库吗?
- c++ - 生成多项式系数的过程和计算它的值的函数
- reactjs - 如何使用 react-router 确保它在 Reactjs 中工作
- android - Platform Exception (error, java.lang.IllegalStateException: A required meta-data tag in your app's AndroidManifest.xml google_maps_flutter not working