- print_r: documentation ( source)
- end: documentation ( source)
- next: documentation ( source)
<?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));