我有一个名为$all_data的数组。
从上述数组中,我正在尝试获取 Chevenez 和 Barcelona 的重叠时间间隔。例如,对于 Chevenez ,如果所有键41.NEwan0,41.NEwan1,42.NEwan0和42.NEwan1都有重叠,则才予以考虑,否则不考虑。
我尝试了此链接中提出的想法。
但它给了我以下输出结果。
Array
(
[Chevenez] => Array
(
[41.NEwan0] => Array
(
[0] => Array
(
[0] => 2022-01-19 03:53:37.49459
[1] => 2022-01-19 04:53:37.49459
)
[1] => Array
(
[0] => 2022-01-09 03:53:37.49459
[1] => 2022-01-09 04:53:37.49459
)
)
[41.NEwan1] => Array
(
[0] => Array
(
[0] => 2022-01-19 04:23:37.49459
[1] => 2022-01-19 05:23:37.49459
)
[1] => Array
(
[0] => 2022-01-09 04:23:37.49459
[1] => 2022-01-09 05:23:37.49459
)
)
[42.NEwan0] => Array
(
[0] => Array
(
[0] => 2022-01-19 04:03:37.49459
[1] => 2022-01-19 04:33:37.49459
)
[1] => Array
(
[0] => 2022-01-09 04:03:37.49459
[1] => 2022-01-09 04:33:37.49459
)
)
[42.NEwan1] => Array
(
[0] => Array
(
[0] => 2022-01-19 04:13:37.49459
[1] => 2022-01-19 05:13:37.49459
)
[1] => Array
(
[0] => 2022-01-09 04:13:37.49459
[1] => 2022-01-09 05:13:37.49459
)
)
)
[Barcelona] => Array
(
[5.NEwan0] => Array
(
[0] => Array
(
[0] => 2022-01-19 03:53:37.49459
[1] => 2022-01-19 04:53:37.49459
)
)
[5.NEwan1] => Array
(
[0] => Array
(
[0] => 2022-01-19 04:23:37.49459
[1] => 2022-01-19 05:23:37.49459
)
)
[16.NEwan0] => Array
(
[0] => Array
(
[0] => 2022-01-19 04:03:37.49459
[1] => 2022-01-19 04:33:37.49459
)
)
[16.NEwan1] => Array
(
[0] => Array
(
[0] => 2022-01-19 04:13:37.49459
[1] => 2022-01-19 05:13:37.49459
)
)
)
)
从上述数组中,我正在尝试获取 Chevenez 和 Barcelona 的重叠时间间隔。例如,对于 Chevenez ,如果所有键41.NEwan0,41.NEwan1,42.NEwan0和42.NEwan1都有重叠,则才予以考虑,否则不考虑。
我尝试了此链接中提出的想法。
// Placeholder array to contain the periods when everyone is available.
$periods = [];
foreach($all_data as $key => $data){
// Loop until one of the people has no periods left.
while (count($data) && count(array_filter($data)) == count($data)) {
// Select every person's earliest date, then choose the latest of these
// dates.
$start = array_reduce($data, function($carry, $ranges) {
$start = array_reduce($ranges, function($carry, $range) {
// This person's earliest start date.
return !$carry ? $range[0] : min($range[0], $carry);
});
// The latest of all the start dates.
return !$carry ? $start : max($start, $carry);
});
// Select each person's range which contains this date.
$matching_ranges = array_filter(array_map(function($ranges) use($start) {
return current(array_filter($ranges, function($range) use($start) {
// The range starts before and ends after the start date.
return $range[0] <= $start && $range[1] >= $start;
}));
}, $data));
// Find the earliest of the ranges' end dates, and this completes our
// first period that everyone can attend.
$end = array_reduce($matching_ranges, function($carry, $range) {
return !$carry ? $range[1] : min($range[1], $carry);
});
// Add it to our list of periods.
$periods[$key][] = [$start, $end];
// Remove any availability periods which finish before the end of this
// new period.
array_walk($data, function(&$ranges) use ($end) {
$ranges = array_filter($ranges, function($range) use($end) {
return $range[1] > $end;
});
});
}
}
// Output the answer in the specified format.
foreach ($periods as $key => $period) {
foreach ($period as $period1) {
echo "$key : $period1[0] -> $period1[1]\n";
}
}
但它给了我以下输出结果。
Chevenez : 2022-01-09 04:23:37.49459 -> 2022-01-09 04:33:37.49459
Chevenez : 2022-01-19 04:03:37.49459 -> 2022-01-19 04:33:37.49459
Barcelona : 2022-01-19 04:23:37.49459 -> 2022-01-19 04:33:37.49459
但我希望得到的输出结果是什么。
Chevenez : 2022-01-09 04:23:37.49459 -> 2022-01-09 04:33:37.49459
Chevenez : 2022-01-19 04:23:37.49459 -> 2022-01-19 04:33:37.49459
Barcelona : 2022-01-19 04:23:37.49459 -> 2022-01-19 04:33:37.49459
这段内容涉及到IT技术,其中提到了时间的错误,应该将第二行中的时间改为 Chevenez: 2022-01-19 04:23:37.49459 -> 2022-01-19 04:33:37.49459。此外,用户 @amirreza-noori 报告说他在以下数组中遇到了错误。
$all_data = [
'Chevenez' => [
'41.NEwan0' => [['2022-01-19 03:53:37.49459', '2022-01-19 04:53:37.49459'],
['2022-01-09 03:53:37.49459', '2022-01-09 04:53:37.49459']],
'41.NEwan1' => [['2022-01-19 04:23:37.49459', '2022-01-19 05:23:37.49459'],
['2022-01-09 04:23:37.49459', '2022-01-09 05:23:37.49459']],
'42.NEwan0' => [['2022-01-19 04:03:37.49459', '2022-01-19 04:33:37.49459'],
['2022-01-09 04:03:37.49459', '2022-01-09 04:33:37.49459']],
'42.NEwan1' => [['2022-01-19 04:13:37.49459', '2022-01-19 05:13:37.49459'],
['2022-01-09 04:13:37.49459', '2022-01-09 05:13:37.49459']],
'43.NEwan0' => [['2022-01-09 03:53:37.49459', '2022-01-09 04:53:37.49459']],
'43.NEwan1' => [['2022-01-09 04:23:37.49459', '2022-01-09 05:23:37.49459']],
'44.NEwan0' => [['2022-01-09 04:03:37.49459', '2022-01-09 04:33:37.49459']],
'44.NEwan1' => [['2022-01-09 04:28:37.49459', '2022-01-09 05:13:37.49459']],
],
'Barcelona' => [
'5.NEwan0' => [['2022-01-19 03:53:37.49459', '2022-01-19 04:53:37.49459']],
'5.NEwan1' => [['2022-01-19 04:23:37.49459', '2022-01-19 05:23:37.49459']],
'16.NEwan0' => [['2022-01-19 04:03:37.49459', '2022-01-19 04:33:37.49459']],
'16.NEwan1' => [['2022-01-19 04:13:37.49459', '2022-01-19 05:13:37.49459']]
]
];
对此的期望输出是什么。
Chevenez : 2022-01-09 04:23:37.49459 -> 2022-01-09 04:33:37.49459
Barcelona : 2022-01-19 04:23:37.49459 -> 2022-01-19 04:33:37.49459