3v4l.org

run code in 300+ PHP versions simultaneously
<?php function group_nums($array) { $ret = array(); $temp = array(); foreach($array as $val) { if(next($array) == ($val + 1)) $temp[] = $val; else if(count($temp) > 0) { $temp[] = $val; $ret[] = $temp[0].'-'.end($temp); $temp = array(); } else $ret[] = $val; } return $ret; } $a = [2,3,4,7,8,9,10,11,13,15]; print_r(group_nums($a));

preferences:
35.9 ms | 402 KiB | 5 Q