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);

preferences:
39.34 ms | 406 KiB | 5 Q