假设我在数组中有以下数字序列:
$numbers = array(1,3,2,23,24,25,26, 8)
如何在范围内打印它们,例如:
数字是 1-3、23-26、8。
这是一个简单的版本,创建groups
包含您的范围
<?php
$numbers = array(1,3,2,23,24,25,26,8);
sort($numbers);
$groups = array();
for($i = 0; $i < count($numbers); $i++)
{
if($i > 0 && ($numbers[$i - 1] == $numbers[$i] - 1))
array_push($groups[count($groups) - 1], $numbers[$i]);
else // First value or no match, create a new group
array_push($groups, array($numbers[$i]));
}
foreach($groups as $group)
{
if(count($group) == 1) // Single value
echo $group[0] . "\n";
else // Range of values, minimum in [0], maximum in [count($group) - 1]
echo $group[0] . " - " . $group[count($group) - 1] . "\n";
}
输出是
1 - 3
8
23 - 26
现在,如果你的范围的顺序很重要,就像你在问题中描述的那样,你仍然可以对你的组进行排序......从我所看到的,你想要范围首先是单个值?这可以通过添加来完成
function groupRanges($a, $b)
{
if(count($a) == 1)
if(count($b) == 1)
return 0; // equal
else
return 1; // so $b is considered less than
if(count($b) == 1)
return -1; // so $a is considered less than
return 0; // both are ranges, keep them there... could be adjusted to compare the size of each range
}
usort($groups, "groupRanges");
就在 and 之前foreach
,输出变为:
1 - 3
23 - 26
8
昨天不能发这个,所以我很晚了。认为它足够优雅,可以分享它。
<?php
$numbers = array(1, 3, 2, 23, 24, 25, 26, 8);
sort($numbers);
$result = array();
while (count($numbers) > 0)
{
$begin = reset($numbers);
$end = array_shift($numbers);
while (in_array($end + 1, $numbers))
{
$end = array_shift($numbers);
}
$beginAndEnd = array_unique(array($begin, $end));
$result[] = implode('-', $beginAndEnd);
}
var_dump($result);
?>
我知道我参加聚会有点晚了,但我不得不这样做。
<?php
$numbers = array(1,3,2,23,24,25,26,8);
sort($numbers);
$x=0;
$output = array();
foreach($numbers as $k=>$n){
if(isset($numbers[$k+1]) && $numbers[$k+1]==$n+1){
$output[$x][]=$n;
}elseif(isset($output[$x][count($output[$x])-1]) && $output[$x][count($output[$x])-1]+1==$n){
$output[$x][]=$n;
}else{
$x++;
$output[$x][] = $n;
$x++;
}
}
foreach($output as $o){
echo $o[0];
if(isset($o[count($o)-1]) && $o[count($o)-1]!=$o[0]){
echo ' - '.$o[count($o)-1];
}
echo '<br>';
}?>
$numbers = array(1,3,2,23,24,25,26,8);
$result = array();
$sorted = $numbers;
sort($sorted);
$current = null;
foreach ($sorted as $v) {
if (is_null($current)) {
$current = array($v, $v);
} else {
if ($current[1] + 1 == $v) {
$current[1] = $v;
} else {
$result[] = $current;
$current = array($v, $v);
}
}
}
$result[] = $current;
$arranged = array();
foreach ($numbers as $v) {
foreach ($result as $k => $r) {
if ($v >= $r[0] && $v <= $r[1]) {
$arranged[] = $r;
unset($result[$k]);
break;
}
}
}
var_dump($arranged);
首先对数组进行排序,比如 array A
。然后对其进行二分查找。说数组的大小n
:
取A[n/2]
, 处的元素n/2
。
if A[n/2]-A[n/4] = n/2-n/4
, then
A[n/2]
和A[n/4]
是否在同一个组中——跳过A[3n/8]
并检查A[n/8]
是否A[n/2]-A[n/8] = n/2-n/8
.
如果A[n/2]-A[n/4] != n/2-n/4
,那么您手头有两组——一组在,另一组
A[n/2]
在A[n/4]
。检查A[n/8]
它是否在同一组中
A[n/4]
。检查A[3/8]
它是否与 同组A[n/2]
,如果不是,是否在组中A[n/4]
。
您也可以将它与排序算法结合使用,但我认为这不值得。这是快速的——在对数时间内并且是模块化的。
$sorted = $numbers;
sort($sorted);
$sorted[] = null; # add a null so the last iteration stores the final range
$ranges = array();
$end = null;
$start = null;
foreach ($sorted as $x) {
if ($start === null) {
# first iteration. New range, but there's no previous one to mention
$start = $x;
}
elseif ($x !== $end + 1) {
# non-contiguous values == new range.
# squirrel away the one we were just working on, and start fresh
$ranges[] = ($start === $end) ? "$start" : "$start-$end";
$start = $x;
}
$end = $x;
}
$ranges_str = implode(', ', $ranges);
function get_ranges($numbers) {
sort($numbers);
$ranges=array();
while(current($numbers)){
$start=current($numbers);
$next=next($numbers);
$current=$start;
while( ($next-$current)==1){
$current=$next;
$next=next($numbers);
}
$ranges[]=array('start'=>$start,'end'=>$current);
}
return $ranges;
}