2

我有以下数组

 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']; 
        }
        }
    }
}
4

3 回答 3

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
        )
)
于 2013-09-02T09:54:21.663 回答
1

用于比较 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 -> 你只需要检查 XY)并且取决于你检查的情况你必须调整您的变量/键选择。

此外,您没有很好地处理夹杂物(排除它们或适当地包括它们)。

简单的例子:

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(start) < B(start) < A(end) == B(end) 等。

于 2013-09-02T09:54:34.177 回答
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;

    }
于 2013-09-02T08:44:46.417 回答