添加Array - PHP的内容

时间:2014-03-15 06:37:40

标签: php arrays math

我有一个包含一大堆分数的数组。这是其中的一小部分。

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 = 

我想把4组中的分数加起来。

我似乎无法找到一种循环数组并添加到正确变量的方法

我用来创建数组$ score的代码是

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

所有数据都在mysql表格中,我使用mysqli来访问它。

提前致谢。

2 个答案:

答案 0 :(得分:1)

PHP实际上对你的用例有一个非常简单的函数:array_sum()将对数组的值求和并返回一个整数或浮点数:

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

<小时/> 如果您可以自由修改MySQL查询,还可以将MySQL SUM()聚合函数与GROUP BY结合使用:

假设您最初有一个查询“布局”(当然这是一个天真的例子)

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

您可以重写它以直接获得teamtype得出的分数:

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

将返回类似

的内容
$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),
);

答案 1 :(得分:0)

这不是那么简单

$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'];