Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to group numbers in ranges using PHP

Let's say that I have the following sequence of numbers in an array:

$numbers = array(1,3,2,23,24,25,26, 8)

How can I print them in ranges, for instance:

The numbers are 1-3, 23-26, 8.

like image 670
Flupkear Avatar asked Dec 07 '22 10:12

Flupkear


1 Answers

Here's a simple version, creating groups containing your ranges

<?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";
}

The output is

1 - 3
8
23 - 26

Now, if the order of your ranges is important, like you described in your question, you can still sort your groups... from what I can see, you want the ranges first followed by the single values? This can be done by adding

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");

right before the foreach and the output becomes:

1 - 3
23 - 26
8
like image 98
emartel Avatar answered Dec 27 '22 02:12

emartel