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!
For making the first array in your update, something like this:
And for your second (“ultimate” :P) example, we just bin the new array, and instead save back into the array we’re looping:
Not tested at all, but I think the logic of it should be pretty much there.