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 3357904
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T02:41:19+00:00 2026-05-18T02:41:19+00:00

This is my first question to SO, I hope to get it right. In

  • 0

This is my first question to SO, I hope to get it right. In PHP (if you can’t, python or pseudo language is also okay), Given an array of n elements:

old_array = [1, 2, 3, 5, 7, 8, 9, 20, 21, 23, 29]

I need to add to a new array consecutive numbers, if it is not a consecutive number add only that value to a new array:

new_array = [ [1,2,3],
              [5],
              [7,8,9]
              [20,21]
              [23],
              [29]
            ]

Here on SO, i found these related topics, but can’t get it to work.

  1. Creating a list of lists with consecutive numbers
  2. Python finding n consecutive numbers in a list
  3. Find the sum of consecutive whole numbers w/o using loop in JavaScript

The code that wasn’t working is on the version history, I removed it because it’s having formatting problems.

Thanks all, and especially Juan, mistabell and Axsuul for the providing the correct answer.

  • 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-05-18T02:41:20+00:00Added an answer on May 18, 2026 at 2:41 am

    The best I can came up with is:

    function subsequenceArray($values) {
        $res = array();
        $length = count($values);
        if (0 == $length) {
            return $res;
        }
        $last = 0;
        $res[$last] = array($values[0]);
        for ($i = 1; $i < $length; $i++) {
            if ($values[$i] == $values[$i-1] + 1) {
                $res[$last][] = $values[$i];
            } else {
                $res[++$last] = array($values[$i]);
            }
        }
        return $res;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

this is my first question here so I hope I can articulate it well
this my first question on here. I hope someone can help. It is to
this is my first question here so I hope I'm doing this right. I
This is my first time here so I hope I post this question at
This is my first question :). Im writing a little twitter app in PHP
I hope this is the appropriate title for this question. Right now I have
Im a new user in stackoverflow and this is my first question hope i
This is my first question here on stackoverflow, so I hope that I am
This is my first question, so I tried to be clear (hope its not
this is my first time asking a question here so I hope I am

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.