3v4l.org

run code in 300+ PHP versions simultaneously
<?php $array = [ [ "08:30", "09:00", "09:30", "10:00", "14:30", "15:00", "15:30", "16:00", "16:30" ], [ "13:30", "14:00", "14:30", "18:00", "18:30", "19:00" ] ]; function compactTimes( array $times, string $format = 'H:i', string $separation = "30 minutes" ): array { $result = []; $prev = null; foreach ($times as $time) { if (!$prev || $time !== date("H:i", strtotime("+$separation $prev"))) { unset($ref); $ref = [$time, $time]; $result[] =& $ref; } else { $ref[1] = $time; } $prev = $time; } return $result; } var_export( array_map('compactTimes', $array) );
Output for 8.1.0 - 8.1.28, 8.2.0 - 8.2.19, 8.3.0 - 8.3.7
array ( 0 => array ( 0 => array ( 0 => '08:30', 1 => '10:00', ), 1 => array ( 0 => '14:30', 1 => '16:30', ), ), 1 => array ( 0 => array ( 0 => '13:30', 1 => '14:30', ), 1 => array ( 0 => '18:00', 1 => '19:00', ), ), )

preferences:
52.07 ms | 402 KiB | 62 Q