I have the following array named $all_data.
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
)
)
)
)
From the above array, I am trying to get overlapping time intervals for Chevenez and Barcelona. Let's say for Chevenez if all keys 41.NEwan0, 41.NEwan1, 42.NEwan0 and 42.NEwan1 has overlap then only consider otherwise not.
I have tried the idea presented at this link.
// 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";
}
}
But it gives me the below output.
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
but my desired output is.
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 gives me Chevenez : 2022-01-19 04:03:37.49459 -> 2022-01-19 04:33:37.49459 in second line but it should be Chevenez : 2022-01-19 04:23:37.49459 -> 2022-01-19 04:33:37.49459
@amirreza-noori I am getting errors for following arrry.
$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']]
]
];
and desired output for it is.
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