Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8923183
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T07:03:06+00:00 2026-06-15T07:03:06+00:00

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

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T07:03:07+00:00Added an answer on June 15, 2026 at 7:03 am

    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
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Let me explain best with an example. Say you have node class that can
Let's say that you have an array of 30 characters and you are looping
Let's say I have a table with a Color column. Color can have various
Let's say that I have a SQLite database that I create in a separate
Let's say I have the following object: var VariableName = { firstProperty: 1, secondProperty:
Let's say I have multiple requirements for a password. The first is that the
Let's say that I have a date in R and it's formatted as follows.
Let say I have the following desire, to simplify the IConvertible's to allow me
Let's say you have a class called Customer, which contains the following fields: UserName
Let's say we have a simple function defined in a pseudo language. List<Numbers> SortNumbers(List<Numbers>

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.