Tim Rideyourbike
Tim Rideyourbike

Reputation: 707

Add contents of Array - PHP

I have an array that contains a whole heap of scores. Here is a small part of it.

Array
(
    [1] => Array
        (
            [plus] => Array
                (
                    [2014-03-15 12:11:15] => 100
                    [2014-03-15 12:14:18] => 100
                    [2014-03-15 12:42:43] => 100
                    [2014-03-15 12:46:29] => 100
                    [2014-03-15 15:19:25] => 500
                    [2014-03-15 15:20:14] => 500
                    [2014-03-15 15:20:15] => 400
                    [2014-03-15 15:20:16] => 300
                    [2014-03-15 15:20:19] => 500
                    [2014-03-15 15:20:20] => 300
                )

            [minus] => Array
                (
                    [2014-03-15 15:19:23] => 500
                    [2014-03-15 15:19:27] => 500
                    [2014-03-15 15:20:12] => 300
                    [2014-03-15 15:20:13] => 400
                    [2014-03-15 15:20:14] => 500
                    [2014-03-15 15:20:18] => 300
                )

        )

    [2] => Array
        (
            [plus] => Array
                (
                    [2014-03-15 12:14:35] => 100
                    [2014-03-15 15:28:58] => 200
                    [2014-03-15 15:28:59] => 300
                    [2014-03-15 15:29:00] => 700
                    [2014-03-15 16:25:50] => 1000
                )

            [minus] => Array
                (
                    [2014-03-15 16:26:10] => 900
                    [2014-03-15 16:26:11] => 100
                    [2014-03-15 16:26:12] => 200
                    [2014-03-15 16:26:13] => 600
                    [2014-03-15 16:26:14] => 800
                )

        )

)

$team1positive = 
$team2positive =
$team1negative = 
$team2negative = 

I am trying to add up the scores the are in the 4 groups.

I just can't seem to find a way to loop through the array and add to the correct variable

The code I used to create the array $scores was

$scores = array();
while ( $line = $result->fetch_assoc() ) {
    $scores[$line['team']][$line['score_type']][$line['date']] = $line['score'];
}

All the data is in a mysql table and I'm using mysqli to access it.

Thanks in advance.

Upvotes: 2

Views: 112

Answers (2)

Lukas
Lukas

Reputation: 1479

PHP actually has a very easy function for your use case: array_sum() will sum the values of an array and return an integer or float:

$team1positive = array_sum($scores[1]['plus']);
$team1negative = array_sum($scores[1]['minus']);
$team2positive = array_sum($scores[2]['plus']);
$team2negative = array_sum($scores[2]['minus']);


If you are free to modify your MySQL query you could also use MySQLs SUM() aggregate function in combination with GROUP BY:

Assuming you originally have a query "layout" like (of course this is a naive example)

SELECT time, score FROM `table_xy` WHERE team = 1 AND type = 'plus';

You could rewrite it to directly get your resulting scores by both team and type:

SELECT team, type, SUM(score) as scores FROM `table_xy` GROUP BY team, type;

Which will return something like

$scores = array(
    0 => array('team' => 1, 'type' => 'plus', 'scores' => 7000), // example value
    1 => array('team' => 1, 'type' => 'minus', 'scores' => 7000),
    2 => array('team' => 2, 'type' => 'plus', 'scores' => 7000),
    3 => array('team' => 2, 'type' => 'minus', 'scores' => 7000),
);

Upvotes: 1

code-jaff
code-jaff

Reputation: 9330

Wouldn't that be simple as this

$scores = array();

while ( $line = $result->fetch_assoc() ) {
    if(!isset($scores[$line['team']][$line['score_type']])) {
        $scores[$line['team']][$line['score_type']] = 0;
    } 
    $scores[$line['team']][$line['score_type']] += $line['score'];
}

$team1positive = $scores[1]['plus'];
$team2positive = $scores[2]['plus'];
$team1negative = $scores[1]['minus'];
$team2negative = $scores[2]['minus'];

Upvotes: 0

Related Questions