<?php function hyphenate(array $numbers) { $result = []; $ref = null; foreach ($numbers as $i => $number) { if ($ref && $number == $numbers[$i - 1] + 1) { $ref = strtok($ref, "-") . "-$number"; } else { unset($ref); $ref = $number; $result[] =& $ref; } } return implode(',', $result); } var_export([ hyphenate(explode(' ', '')), hyphenate(explode(' ', '1')), hyphenate(explode(' ', '1 2')), hyphenate(explode(' ', '1 3')), hyphenate(explode(' ', '1 3 4 6')), hyphenate(explode(' ', '1 3 4 6 7')), hyphenate(explode(' ', '1 2 3 4 5')), hyphenate(explode(' ', '1 2 3 5 6 10 12 13')), ]);
You have javascript disabled. You will not be able to edit any code.
Value for `_results` contains invalid data `array`