我搞不懂怎么用短表格表示序号
我有一个数组元素,如
$a = [2,3,4,7,8,9,10,11,13,15]但我需要输出
2-4, 7-11, 13, 15你能不能有人建议如何用PHP做这件事?谢谢
发布于 2014-10-29 16:16:43
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;
} 测试
https://stackoverflow.com/questions/26635299
复制相似问题