3v4l.org

run code in 300+ PHP versions simultaneously
<?php $lookup = array_flip(['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec']); $months = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Sep', 'Oct', 'Dec', 'Jan', 'Feb']; $result = []; foreach ($months as $month) { if (isset($ref) && $lookup[$month] === ($lookup[$last] + 1) % 12) { $ref = strtok($ref, "-") . "-$month"; } else { unset($ref); $ref = $month; $result[] = &$ref; } $last = $month; } var_export($result);
Output for 8.0.1 - 8.0.30, 8.1.0 - 8.1.33, 8.2.0 - 8.2.29, 8.3.0 - 8.3.27, 8.4.1 - 8.4.14
array ( 0 => 'Jan-Jun', 1 => 'Sep-Oct', 2 => 'Dec-Feb', )

preferences:
106.31 ms | 406 KiB | 5 Q