How do I sequentially add numbers from different arrays?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
zachalbert
Forum Newbie
Posts: 13
Joined: Sun Jan 04, 2009 11:32 pm

How do I sequentially add numbers from different arrays?

Post by zachalbert »

So I have a series of arrays (around 65) that I need to add together. But I need to add the respective values while maintaining the original value it was derived from. In other words, this is what I have now:

Code: Select all

 
array(0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0);
array(1,-1,-1,-1,-1,-1,-1,-1,1,1,-1,0,-1,1,1,1,1,1,-1,-1,-1,1,1,-1,-1,0,1,0,1,0,0);
array(1,1,1,1,0,-1,-1,1,1,1,0,0,1,0,1,1,1,1,0,1,1,0,1,1,1,1,1,0,1,0,0);
 
Each array has a set number of values (31). I need to add the respective values from all the arrays so it looks like this:

Code: Select all

 
array(0,0,0,0,0,0,0,0,...
array(1,-1,-1,-1,-1,-1,-1,-1,1,...
array(2,0,0,0,-1,-1,-1,0,... // notice the first column: 0, 1, 1 becomes 0, 1, 2
 
So the first value of the array is the sum of itself and the first value from the previous array. And so on 65 times.

I am really confused with how to structure my loops. I think the array itself should have a counted value (array['1-65']) and then the array's values should also be counted (array['1-31']), but I'm not sure how to count both the values within an array and multiple arrays themselves, let alone start adding their respective values.

Am I looking in the right place for how to do this? Or is my approach wrong?
User avatar
Chewbacca
Forum Newbie
Posts: 5
Joined: Fri Jan 23, 2009 1:57 pm

Re: How do I sequentially add numbers from different arrays?

Post by Chewbacca »

Here's a start:

Code: Select all

<?php
 
function sumArrayValuesByKey(array $a) {
    $aCount = count($a);
    for ($i = 1; $i < $aCount; $i++) {
        foreach ($a[$i] as $k => &$v) {
            $v += $a[$i - 1][$k];
        }
    }
    return $a;
}
 
function displayArray(array $ar) {
    echo '<table>';
    foreach ($ar as $a) {
        echo '<tr>';
        foreach ($a as $v) {
            echo '<td align="right">' . $v . '</td>';
        }
        echo '</tr>';   
    }
    echo '</table>';
}
 
$ar = array(
    array(0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0),
    array(1,-1,-1,-1,-1,-1,-1,-1,1,1,-1,0,-1,1,1,1,1,1,-1,-1,-1,1,1,-1,-1,0,1,0,1,0,0),
    array(1,1,1,1,0,-1,-1,1,1,1,0,0,1,0,1,1,1,1,0,1,1,0,1,1,1,1,1,0,1,0,0)
);
 
// Show the unmodified array
displayArray($ar);
 
echo '<hr />';
 
// Show the modified array
displayArray((sumArrayValuesByKey($ar)));
zachalbert
Forum Newbie
Posts: 13
Joined: Sun Jan 04, 2009 11:32 pm

Re: How do I sequentially add numbers from different arrays?

Post by zachalbert »

Awesome, that worked perfectly. Unfortunately, and I realize I should've specific this before, I need this information displayed a bit differently. The data for the array has to come from a database with an undefined number of rows, and I can't figure out how to get a while loop to correctly execute within an array.

First, here's what my ultimate goal is.

Code: Select all

 
// There is one of these lines for each row in the table, with the numbers from each array the sum of itself and the line previous
$DataSet->AddPoint(array(1,1,0,-1,1,0,0,-1...),"Serie1");
$DataSet->AddPoint(array(2,1,-1,-2,2,0,1,0...),"Serie2");
// And so on...
 
So in the example you gave, the original arrays were all specified within the variable $ar. I have tried both to wrap the while loop around the $ar declaration (resulting in only the last table row populating the variable) and putting the while loop within $ar's array parenthesis (resulting in something doesn't work at all). I can't figure out how to make the array and the loop work with each other.

With the help of tasairis, the following code accurately displays all the lines with the array section populated with the original values. How do I combine this code with yours?

Code: Select all

 
$k = 1;
while($row = mysql_fetch_array( $result )) {
    $DataSet->AddPoint($row, "Serie" . $k);
    $k++;
}
 
Post Reply