# Aggregate array values into ranges

In five minutes I made a pretty ugly looking function. Can you help before I have to commit the code into history?

Requirements:

I would like a function that takes an array of numbers, and returns an array of ranges. Ideally remove the duplicate line.

Sample Input:

Array(1,2,3,4,5,6,10,11,12,13,20,24)


Output:

Array
(
 => 1-6
 => 10-13
 => 20
 => 24
)


Winning Entries:

Pseudo code
PHP


My attempt:

$myArray = array(1,2,3,4,5,6,10,11,12,13,20,24);$rangeArray = array();
$start =$end = current($myArray); foreach($myArray as $range){ if($range - $end > 1){$rangeArray[] = ($start ==$end)?$start:$start."-".$end;$start = $range; }$end = $range; }$rangeArray[] = ($start ==$end)?$start:$start."-".$end; print_r($rangeArray);

• I don't see a better way to do it more efficiently in an algorithmic meaning. This kind of task must go straight to the point and that is exactly what you have done. However if you project to build a function, you will need to check the parameters, to define default behaviors and to throw exceptions for inappropriate types of data, that's all. – Casimir et Hippolyte Feb 14 '15 at 10:01

While these may not be any shorter or more efficient than existing answers, perhaps with a slightly different approach they still bring something to the table and might be considered a little less 'ugly'.

Essentially the GetRanges function I've written does the following:

1. Take a parameter-supplied array of numbers ($aNumbers), remove any duplicates from the array and then sort them numerically lowest to highest. 2. We next create a multi-dimensional array ($aGroups) and working sequentially through the array of numbers ($aNumbers): Except for the first number which we just add to $aGroups to get us started, for each consecutive number $i > 0, if the previous number $aNumbers[$i-1] is equal to the current number minus 1 $aNumbers[$i] - 1 then the current number is added to the same group because its consecutive and belongs to that range, otherwise the current number is added to $aGroups as a new sub-array with one entry array( $aNumbers[$i] ).

For options 2 and 3 where the function is expanded upon for a simple array:

1. A new array ($aRanges) is created and for each $aGroups record, if the first entry of the sub-array is the only entry then a new entry is added to $aRanges for just this number, otherwise a new entry is added to $aRanges specifying the first number in the range followed by a hyphen followed by the last number in the range.

For option 3 where the function is expanded upon to return a string:

1. The implode function is used to create a comma-separated list of all the entries in the $aRanges array and return this as a string. Option 1: If you want your function to return a multi-dimensional array: <?php function GetRanges($aNumbers ) {
$aNumbers = array_unique($aNumbers );
sort( $aNumbers );$aGroups = array();
for( $i = 0;$i < count( $aNumbers );$i++ ) {
if( $i > 0 && ($aNumbers[$i-1] ==$aNumbers[$i] - 1 )) array_push($aGroups[count($aGroups)-1],$aNumbers[$i] ); else array_push($aGroups, array( $aNumbers[$i] ));
}
return $aGroups; }$aNumbers = array( 1, 2, 3, 4, 5, 6, 10, 11, 12, 13, 20, 24 );
print_r( GetRanges( $aNumbers ));  Output: Array (  => Array (  => 1,  => 2,  => 3,  => 4,  => 5,  => 6 )  => Array (  => 10,  => 11,  => 12,  => 13 )  => Array (  => 20 )  => Array (  => 24 ) )  Option 2: If you want your function to return a simple array: <?php function GetRanges($aNumbers ) {
$aNumbers = array_unique($aNumbers );
sort( $aNumbers );$aGroups = array();
for( $i = 0;$i < count( $aNumbers );$i++ ) {
if( $i > 0 && ($aNumbers[$i-1] ==$aNumbers[$i] - 1 )) array_push($aGroups[count($aGroups)-1],$aNumbers[$i] ); else array_push($aGroups, array( $aNumbers[$i] ));
}
$aRanges = array(); foreach($aGroups as $aGroup ) { if( count($aGroup ) == 1 )
$aRanges[] =$aGroup;
else
$aRanges[] =$aGroup . '-' . $aGroup[count($aGroup)-1];
}
return $aRanges; }$aNumbers = array( 1, 2, 3, 4, 5, 6, 10, 11, 12, 13, 20, 24 );
print_r( GetRanges( $aNumbers ));  Output: Array (  => 1-6,  => 10-13,  => 20,  => 24 ) Option 3: If you want your function to return a string: <?php function GetRanges($aNumbers ) {
$aNumbers = array_unique($aNumbers );
sort( $aNumbers );$aGroups = array();
for( $i = 0;$i < count( $aNumbers );$i++ ) {
if( $i > 0 && ($aNumbers[$i-1] ==$aNumbers[$i] - 1 )) array_push($aGroups[count($aGroups)-1],$aNumbers[$i] ); else array_push($aGroups, array( $aNumbers[$i] ));
}
$aRanges = array(); foreach($aGroups as $aGroup ) { if( count($aGroup ) == 1 )
$aRanges[] =$aGroup;
else
$aRanges[] =$aGroup . '-' . $aGroup[count($aGroup)-1];
}
return implode( ',', $aRanges ); }$aNumbers = array( 1, 2, 3, 4, 5, 6, 10, 11, 12, 13, 20, 24 );
echo( GetRanges( $aNumbers ));  Output: 1-6,10-13,20,24 • Please consider editing your answer to explain more about your code. You should try to explain all the suggestions you're making, how they work and what difference they might make. – SuperBiasedMan Sep 4 '15 at 11:19 • @SuperBiasedMan I've now added detailed explanation to the answer of how the code works. In terms of difference it might make, I suspect absolutely nothing other than perhaps the code is easier to read and understand. – richhallstoke Sep 4 '15 at 12:03 • For the record, readability is a perfectly valid reason to give someone when you review their code (just in case my comment seemed to imply otherwise). – SuperBiasedMan Sep 4 '15 at 13:15 You could add a null value at the end of your main array and remove last line. This way the null will be dropped, but it probably will be harder to understand if someone comes later to see your code. Maybe better naming for your vars. I played a bit with the problem, here's what I got. The idea is the same wrote in a different way. $myArray = array(1,2,3,4,5,6,10,11,12,13,20,24);

//last value is dropped so add something useless to be dropped
array_push($myArray, null);$rangeArray = array();

array_walk($myArray, function($val) use (&$rangeArray){ static$oldVal, $rangeStart; if (is_null($rangeStart))
goto init;

if ($oldVal+1 ==$val) {
$oldVal =$val;
return;
}

if ($oldVal ==$rangeStart) {
array_push($rangeArray,$rangeStart);
goto init;
}

array_push($rangeArray,$rangeStart . '-' . $oldVal); init: {$rangeStart = $val;$oldVal = $val; } }); print_r($rangeArray);

• Thank you for your input. I am not a big fan of the goto statement and therefore usually stay away, with a little bit of refactoring it can be removed. Interesting idea to add a null value to remove the need to duplicate the array_push line. – William George Feb 11 '15 at 19:36
• Hi @WilliamGeorge, if you like this (and other answers too, as per your other comments), why not reward them with upvotes? – janos Feb 15 '15 at 11:45

I've changed the output slightly to make the processing easier:

$numbers = array(1,2,3,4,5,6,10,11,12,13,20,24);$ranges[] = array($numbers,$numbers); // initial value

foreach ($numbers as$number) {
$range = array_pop($ranges);
$extend = ($range == $number-1);$ranges[] = array($range,$extend ? $number :$range);
if (!$extend)$ranges[] = array($number,$number);
}

echo '<pre>'.print_r($ranges,TRUE).'</pre>';  It's now easy to use this output for whatever purpose you need it. It looks like this: Array (  => Array (  => 1  => 1 )  => Array (  => 1  => 6 )  => Array (  => 10  => 13 )  => Array (  => 20  => 20 )  => Array (  => 24  => 24 ) )  If you really need to, you could get your output by using this code: foreach ($ranges as $range) {$output[] = ($range ==$range) ? $range :$range.'-'.$range; } echo '<pre>'.print_r($output,TRUE).'</pre>';


Which will result in:

Array
(
 => 1
 => 1-6
 => 10-13
 => 20
 => 24
)


more like an alternative to give some impulse than a review of your code

<?php
$myArray = array(1,2,3,4,5,6,10,11,12,13,20,24);$rangeArray = array();

class ArrayAggregator implements Iterator {

private $data; private$index = 0;

public function __construct(array $array) {$this->data = $array; } public function current() { // save the start value$startValue = $this->_current(); // iterate until gap is bigger than 1 do { // get the "real" current value$value = $this->_current(); // move to the next value$this->next();
// until gap
} while ($this->_current() ==$value +1);

// @todo more grace
// set index back because foreach will call "next" on after we return the current value
$this->index -= 1; // return start and end value as aggregate output return sprintf('%d - %d',$startValue, $value); } private function _current() { return$this->data[$this->index]; } public function next() {$this->index += 1;
}

public function key()
{
return $this->index; } public function valid() { return isset($this->data[$this->index]); } public function rewind() {$this->index = 0;
}
}

$aa = new ArrayAggregator($myArray);
foreach ($aa as$a) {
echo \$a."\n";
}

• Wow, thats a fun class. I never would have thought to do that. A little overkill for my specific needs, but I like it :) – William George Feb 11 '15 at 20:14