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);
Output for 7.2.0 - 7.2.33, 7.3.0 - 7.3.33, 7.4.0 - 7.4.33, 8.0.0 - 8.0.30, 8.1.0 - 8.1.33, 8.2.0 - 8.2.29, 8.3.0 - 8.3.26, 8.4.1 - 8.4.13
Array ( [2020-02-07] => Array ( [0] => 09:00 [1] => 10:30 - 11:00 ) [2020-02-08] => Array ( [0] => 09:00 - 09:30 [1] => 09:30 - 10:00 ) [2020-02-09] => Array ( [0] => 09:00 - 09:30 [1] => 11:00 ) [2020-02-10] => Array ( [0] => 09:00 [1] => 10:30 [2] => 11:30 ) )

preferences:
136.37 ms | 408 KiB | 5 Q