我有以下数组
Array
(
[0] => Array
(
[from_time] => 15:00
[to_time] => 17:15
)
[1] => Array
(
[from_time] => 10:00
[to_time] => 12:15
)
[2] => Array
(
[from_time] => 09:00
[to_time] => 11:15
)
[3] => Array
(
[from_time] => 09:00
[to_time] => 11:15
)
[4] => Array
(
[from_time] => 14:00
[to_time] => 16:15
)
[5] => Array
(
[from_time] => 15:00
[to_time] => 17:15
)
)
我想在这个阵列中获得共同的时间。
如果我在这个数组中找到共同的时间,我的预期结果应该是
Array
(
[0] => Array
(
[from_time] => 10:00
[to_time] => 11:15
)
[1] => Array
(
[from_time] => 15:00
[to_time] => 16:15
)
)
我尝试使用以下代码但未获得准确的结果。
foreach ($booking_time as $time1) {
foreach ($booking_time as $time2) {
{
if(($time1['from_time'] > $time2['from_time'] && $time1['from_time'] < $time2['to_time'])|| ( $time1['to_time'] > $time2['from_time'] && $time1['to_time'] < $time2['to_time'])){
echo $time1['from_time'];
echo $time1['to_time'];
}
}
}
}
答案 0 :(得分:4)
快速示例(我认为它可以更短,但为了快速理解它可以):
$intervals = [
['from_time' => '15:00', 'to_time' => '17:15'],
['from_time' => '10:00', 'to_time' => '12:15'],
['from_time' => '09:00', 'to_time' => '11:15'],
['from_time' => '09:00', 'to_time' => '11:15'],
['from_time' => '14:00', 'to_time' => '16:15'],
['from_time' => '15:00', 'to_time' => '17:15'],
];
$overlaps = [];
foreach ($intervals as $interval) {
$key = null;
foreach ($overlaps as $_key => $_intervals) {
foreach ($_intervals as $_interval) {
if (
($_interval['from_time'] <= $interval['from_time'] && $interval['from_time'] <= $_interval['to_time'])
||
($_interval['from_time'] <= $interval['to_time'] && $interval['to_time'] <= $_interval['to_time'])
) {
$key = $_key;
break 2;
}
}
}
if (is_null($key)) {
$key = count($overlaps);
}
$overlaps[$key][] = $interval;
}
foreach ($overlaps as &$overlap) {
$from = '00:00';
$to = '23:59';
foreach ($overlap as $_interval) {
$from = max($from, $_interval['from_time']);
$to = min($to, $_interval['to_time']);
}
$overlap = ['from_time' => $from, 'to_time' => $to];
}
unset($overlap);
print_r($overlaps);
输出:
Array
(
[0] => Array
(
[from_time] => 15:00
[to_time] => 16:15
)
[1] => Array
(
[from_time] => 10:00
[to_time] => 11:15
)
)
答案 1 :(得分:0)
你想要增加或+ = [任何数字]
$latest = aray_pop($booking_time); // this will grab the last element from the array
for($i = 0; $i < $latest; $i += 15 // this is the number you want to increment by) {
$booking_time[$key] = $i;
}
答案 2 :(得分:0)
用于比较A和B的常规叠加
X) A(start) < B(start) < A(end) < B(end)
Y) B(start) < A(start) < B(end) < A(end)
(并且通常也需要完整的包含物)
B(start) <= A(start) < A(end) <= B(end)
A(start) <= B(start) < B(end) <= A(end)
你实际上写下了这一点,但你忽略了A和B在你的来源中切换位置(A将在另一次运行时为B和BA - &gt;你只需要检查X 或 Y )根据您检查的情况,您必须调整变量/键选择。
此外,您没有很好地处理包含物(包括它们或适当地包括它们)。
简单示例:
a = 10-20 b = 15-25
你希望结果是15-20
让我们假设运行:time1 a,time2 b
$time1['to_time'] > $time2['from_time'] &&
$time1['to_time'] < $time2['to_time']
匹配并打印
10-20 (time1 (a) from and to -- you wanted to print time2 from and time1 to)
另一次运行将指定:time2 a和time1 b
$time1['from_time'] > $time2['from_time'] &&
$time1['from_time'] < $time2['to_time'])
匹配并打印
15-25 (time1 (b) from and to -- you wanted to print time1 from and time2 to)
这是一个工作示例
foreach($array as $key => $time1) {
foreach($array as $key2 => $time2) {
if (($time1["from_time"] < $time2["from_time"]) && ($time2["from_time"] < $time1["to_time"]) && ($time2["to_time"] > $time1["to_time"])) {
$overlap[] = array("from_key" => $time2["from_time"], "to_time" => $time1["to_time"]);
}
//inclusion
if (($time1["from_time"] >= $time2["from_time"]) && ($time2["to_time"] >= $time1["to_time"]) && ($key !== $key2)) {
$overlap[] = array("from_time" => $time1["from_time"], "to_time" => $time1["to_time"]);
}
}
}
免责声明:没有双重条目的检查。如果你不想要包含,你需要一种方法来处理A(开始)&lt; B(开始)&lt; A(结束)== B(结束)等。