3v4l.org

run code in 300+ PHP versions simultaneously
<?php $array = [ '2020-02-07' => ['09:00' => 1500, '10:30' => 1500, '11:00' => 1500], '2020-02-08' => ['09:00' => 1500, '09:30' => 1500, '10:00' => 1500], '2020-02-09' => ['09:00' => 1500, '09:30' => 1500, '11:00' => 1500], '2020-02-10' => ['09:00' => 1500, '10:30' => 1500, '11:30' => 1500] ]; $result = array(); foreach ($array as $date => $arr) { $next_included = false; $times = array_keys($arr); $count = count($times); for ($i = 0; $i < $count - 1; $i++) { $current = $times[$i]; $next = $times[$i+1]; if (strtotime($next) - strtotime($current) == 1800) { $result[$date][] = "$current - $next"; $next_included = true; } else { if (!$next_included) $result[$date][] = $current; $next_included = false; } } if (!$next_included) { $result[$date][] = $next; } } print_r($result);

preferences:
25.27 ms | 406 KiB | 5 Q