I want to write a function which must find the overlapping time that two or more people can visit each other at the available time. Let me explain:
Here's is my data:
$availability = [
[
["8:30", "12:00"],
["17:00", "22:00"]
],
[
["5:00", "11:15"],
["14:25", "20:05"]
]
];
and I want this result:
RESULT:
[
('8:30', '11:15'),
('17:00', '20:05')
]
My problem is that i can only get the latest result ('17:00', '20:05') not the first one. blank result:
$availability = [
[
["8:30", "12:00"],
["17:00", "22:00"]
],
[
["5:00", "11:15"],
["14:25", "20:05"]
]
];
$periods = [];
while (true) {
$start = array_reduce($availability, function ($carry, $ranges) {
$start = array_reduce($ranges, function ($carry, $range) {
return !$carry ? $range[0] : min($range[0], $carry);
});
return !$carry ? $start : max($start, $carry);
});
$matching_ranges = array_filter(array_map(function ($ranges) use ($start) {
return current(array_filter($ranges, function ($range) use ($start) {
return $range[0] <= $start && $range[1] >= $start;
}));
}, $availability));
if (count($matching_ranges) < count($availability)) {
break;
}
$end = array_reduce($matching_ranges, function ($carry, $range) {
return !$carry ? $range[1] : min($range[1], $carry);
});
$periods[] = [$start, $end];
array_walk($availability, 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 $period) {
echo "$period[0] -> $period[1]\n";
}
Thanks in advance.
source https://stackoverflow.com/questions/68103327/find-users-who-can-visit-each-other-on-same-available-time-time-overlapping
Comments
Post a Comment