1 回答
TA贡献1804条经验 获得超2个赞
首先,您需要按开始日期排序结果。在您需要实现日期起止期间的交集之后。
假设它已经完成,代码可能与此类似:
<?php
$bookingData = [
[
'room_type' => '1-A',
'start' => '2019-10-04',
'end' => '2019-10-14',
],
[
'room_type' => '1-A',
'start' => '2019-10-13',
'end' => '2019-10-22',
],
[
'room_type' => '1-A',
'start' => '2019-11-30',
'end' => '2019-12-18',
],
[
'room_type' => '1-A',
'start' => '2019-12-16',
'end' => '2019-12-25',
],
];
$output = [];
$k = -1;
foreach ($bookingData as $data) {
$curr = $output[$k] ?? null;
if ($curr !== null &&
$data['start'] >= $curr['start'] &&
$data['start'] <= $curr['end'] &&
$data['end'] > $curr['end']) {
$output[$k]['end'] = $data['end'];
} else {
$output[] = $data;
$k++;
}
}
foreach ($output as $item) {
echo sprintf("start_date => %s AND end_date => %s\r\n", $item['start'], $item['end']);
}
- 1 回答
- 0 关注
- 107 浏览
添加回答
举报