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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T10:56:16+00:00 2026-05-29T10:56:16+00:00

id like to know how I can execute this. say i have an array

  • 0

id like to know how I can execute this.

say i have an array of values
[0] 123
[1] 23242
[2] 123123
[3] 134234
[4] 0
[5] 12312
[6] 1232
[7] 0
[8] 2342
[9] 0

How can i loop through this array and everytime it hits a zero, push into a new array, the sum of the preceeding values up to the last 0

eg….
my new array would contain.
[0] sum of first array keys [0-4]
[1] sum of [5-7]
[2] sum of [8-9]

Im new to PHP and cant wrap my head around how I would do this.
Like how can I look at previous values while looking through an array

thanks if anyone can help
I appreciate it

UPDATE:
So Joe wanted me to update this so he could help me further, so here it is…

I want to loop through and array, have the iterator do the math to find the sums between the zeros, and store in a new array, the value, and a running total. And then I want to be able to merge it back into the original array….eg
How do I do the running total along with the new array.

       Loop array        New Array, with comma delimitted values or maybe a MDA
       [0]5              [0]9,9  (sum of values in loop array between the zeros)
       [1]4              [1]7,16
       [2]0              [2]4,20 
       [3]3              [3]5,25
       [4]2 
       [5]2
       [6]0
       [7]4
       [8]0
       [9]3
       [10]2
       [11]0

And finally, the most important,
How do I merge it back so it will look like the following

       [0]5             
       [1]4             
       [2]0,9,9            
       [3]3              
       [4]2 
       [5]2
       [6]0,7,16
       [7]4
       [8]0,4,20
       [9]3
       [10]2
       [11]0,5,25

Thank you if you can help me!

  • 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-29T10:56:17+00:00Added an answer on May 29, 2026 at 10:56 am
    $total = 0; // running total
    $totals = array(); // saved totals
    
    foreach ($values AS $value) // loop over the values
    {
        $total += $value; // add to the running total
        if ($value == 0) // if it's a zero
        {
            $totals[] = $total; // save the total...
            $total = 0; // ...and reset it
        }
    }
    

    For making the first array in your update, something like this:

    $total = 0; // running total - this will get zeroed
    $grand_total = 0; // running total - this won't be zeroed
    $totals = array(); // saved totals
    
    foreach ($values AS $value) // loop over the values
    {
        $total += $value; // add to the running total
        $grand_total += $value; // add it to the grand total
        if ($value == 0) // if it's a zero
        {
            $totals[] = $total . ',' . $grand_total; // save the total and the grand_total
            $total = 0; // ...and reset the zeroable total
        }
    }
    

    And for your second (“ultimate” :P) example, we just bin the new array, and instead save back into the array we’re looping:

    $total = 0; // running total - this will get zeroed
    $grand_total = 0; // running total - this won't be zeroed
    
    foreach ($values AS $key => $value) // loop over the values - $key here is the index of the current array element
    {
        $total += $value; // add to the running total
        $grand_total += $value; // add it to the grand total
        if ($value == 0) // if it's a zero
        {
            $values[$key] = '0,' . $total . ',' . $grand_total; // build the new value for this element
            $total = 0; // ...and reset the zeroable total
        }
    }
    

    Not tested at all, but I think the logic of it should be pretty much there.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I would like to know if I can install say Visual Studio 2008 Pro
Say I have an entity that looks something like this simple example: MyEntity {
Let's say I have a class that implements the IDisposable interface. Something like this:
Didn't know how else to put this. Say I have a JavaScript method that
I have a problem.... Lest say I have a class like this one: public
Say I have a method like this: public void SaveData() { try { foreach
i would like know some reference. I know i can googling it. but prefer
I would like to know where can I find the code which eclipse uses
I would like to know how Can I paas Page as Ref Parameter to
I would like to know how can i read a value of input text

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.