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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T00:38:34+00:00 2026-06-10T00:38:34+00:00

How to group all differences between consecutive numbers from associative array? Below is the

  • 0

How to group all differences between consecutive numbers from associative array? Below is the array which I’m using

need to group (2,3)(7,8) with they difference , ignore (11,6,5,9)

$array = array(
    2 =>"12",
    3 =>"12",
    11 => "16",
    6 => "15",
    5 => "14",
    7 => "16",
    8 => "17",
    9 => "18")

Below is the code which I’m using to group the array, but it’s not working:

foreach ($array as $k => $v) {
    echo  $prev;
    if (isset($prev)) {
        if (($v - $prev) != 1) $newArray[] = $v;
    } else { $newArray[] = $v; }
    $prev = $v;
}
echo '<pre>';
print_r($newArray);

Currently I’m getting this:

Array
(
    [0] => 12
    [1] => 12
    [2] => 16
    [3] => 15   
    [4] => 16
)

but I need the O/P to be (2,3)=0 difference, (7,8)=1 difference, i. e.

array(
    [0]=>0,
    [1]=>1
);
  • 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-10T00:38:36+00:00Added an answer on June 10, 2026 at 12:38 am

    So here you go.

    $newArray = array();
    foreach ($array as $k => $v) {
        if (isset($prev) && $k == $prev + 1) {
            $newArray[] = $v - $array[$prev];
        }
        $prev = $k;
    }
    

    This produces the array for three consecutive pairs:

    Array
    (
        [0] => 0
        [1] => 1
        [2] => 1
    )
    

    If you want to stop after two pairs, add a running counter $i = 0; before the foreach and exit with if (++$i >= 2) break; inside the if in the loop:

    $newArray = array();
    $i = 0;
    foreach ($array as $k => $v) {
        if (isset($prev) && $k == $prev + 1) {
            $newArray[] = $v - $array[$prev];
            if (++$i >= 2) break;
        }
        $prev = $k;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to get all group members from Domain Users. When using AD
(I'm also asking this on the OpenRasta google group) Hey all, I've been using
I've been using GROUP BY for all types of aggregate queries over the years.
I need a difference between two values based on their row number. The CTE
Can anyone tell me, what's the real difference between group and role? I've been
When I try to get the difference between two apparently equal numbers, I get
Could anyone explain to me the difference between the SQL statements below? I can
What I want to do: Group all the like elements on a page (of
I Group By all the deals in my database by store. How can I
The following line in my C program should provided All/Group/Owner read and write permissions

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.