<?php $properties_no = ['1021-5150', '1021-5152', '1021-5153', '1021-5154', '1021-5156', '1021-5157', '1021-5158', '1021-5159', '1021-5161', '1021-5162', '1021-5163']; final class Range { private string $from; private string $to; public function __construct(string $from, string $to) { $this->from = $from; $this->to = $to; } public function __toString(): string { return $this->from === $this->to ? $this->from : "{$this->from} to {$this->to}"; } } /** * @param string[] $values * @return Range[] */ function computeRanges(array $values): array { if (count($values) === 0) { return []; } $ranges = []; $first = $previous = $values[0]; $previousIntValue = (int)str_replace('-', '', $first); foreach (array_slice($values, 1) as $value) { $intValue = (int)str_replace('-', '', $value); if ($intValue > $previousIntValue + 1) { $ranges[] = new Range($first, $previous); $first = $value; } $previous = $value; $previousIntValue = $intValue; } $ranges[] = new Range($first, $previous); return $ranges; } echo implode(PHP_EOL, computeRanges($properties_no));
You have javascript disabled. You will not be able to edit any code.
Value for `_results` contains invalid data `array`