https://codereview.stackexchange.com/questions/80080/aggregate-array-values-into-ranges
static function GetRanges1( $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;
}
static function GetRanges2( $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[0];
else
$aRanges[] = $aGroup[0] . '-' . $aGroup[count($aGroup)-1];
}
return $aRanges;
}
static function GetRanges3( $aNumbers, $headChar='' ) {
$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[] = $headChar.$aGroup[0];
else
$aRanges[] = $headChar.$aGroup[0] . '~' . $headChar.$aGroup[count($aGroup)-1];
}
return implode( ',', $aRanges );
}