3v4l.org

run code in 200+ php & hhvm versions
Bugs & Features
<?php $a = [1,2,3,4,13]; $nxt = $a[0]; $cnt = count($a); $j = 0; $ret = []; for ($i = 1; $i < $cnt; ++$i) { $pre = $nxt; $diff = $nxt; // ---- WAIT LIKE AGES FOR $a[$i] $nxt = $a[$i]; //$k = $a[$i + 1]; //$diff = $nxt - $pre; // MOV diff,nxt SUB diff,pre LATENCY: 2 //$diff = $pre - $nxt; $diff -= $nxt; $ret[$j++] = $pre; // branch if ($diff == -1) { // raising range do { ++$nxt; ++$i; } while ($a[$i] == $nxt); --$nxt; $ret[$j] = "$pre-$nxt";// . ($nxt - 1); } elseif ($diff == 1) { // found falling range ++$i; while ($a[$i] == --$nxt) { // search end of range ++$i; } $ret[$j] = "$pre-" . ($nxt + 1); $nxt = $a[$i]; } }
based on tDign
Output for 5.6.0 - 5.6.30, hhvm-3.12.14 - 3.17.3, 7.0.0 - 7.3.0rc3