【问题标题】:PHP Sum multidimensional array values where certain index is the samePHP对某个索引相同的多维数组值求和
【发布时间】:2020-07-08 17:33:37
【问题描述】:

这里只是一个简单的问题。我有以下数组:

Array(21) {
[0] => Array(7) {
    ["punti"] => Integer  418
    ["vittorie"] => Integer  9
    ["podi"] => Integer  18
    ["gv"] => Integer  14
    ["id_pilota"] => Integer  1
    ["team"] => String(15) "Red Bull Racing"
    ["naz"] => String(2) "it"
}
[1] => Array(7) {
    ["punti"] => Integer  353
    ["vittorie"] => Integer  6
    ["podi"] => Integer  16
    ["gv"] => Integer  3
    ["id_pilota"] => Integer  19
    ["team"] => String(16) "Scuderia Ferrari"
    ["naz"] => String(2) "it"
}
[2] => Array(7) {
    ["punti"] => Integer  335
    ["vittorie"] => Integer  4
    ["podi"] => Integer  15
    ["gv"] => Integer  1
    ["id_pilota"] => Integer  5
    ["team"] => String(12) "Mercedes-AMG"
    ["naz"] => String(2) "it"
}
[3] => Array(7) {
    ["punti"] => Integer  181
    ["vittorie"] => Integer  1
    ["podi"] => Integer  5
    ["gv"] => Integer  1
    ["id_pilota"] => Integer  2
    ["team"] => String(12) "Mercedes-AMG"
    ["naz"] => String(2) "it"
}
[4] => Array(7) {
    ["punti"] => Integer  147
    ["vittorie"] => Integer  0
    ["podi"] => Integer  3
    ["gv"] => Integer  0
    ["id_pilota"] => Integer  14
    ["team"] => String(15) "Racing Point F1"
    ["naz"] => String(2) "mx"
}
[5] => Array(7) {
    ["punti"] => Integer  127
    ["vittorie"] => Integer  0
    ["podi"] => Integer  0
    ["gv"] => Integer  0
    ["id_pilota"] => Integer  13
    ["team"] => String(7) "Haas F1"
    ["naz"] => String(2) "dk"
}

减少到 21 个元素。

我的目标是根据每个飞行员的团队总结所有分数(“punti”索引)。 所以要获得这样的东西:

Array(10) {
[0] => Array(2) {
    ["team"] => String(...) "Mercedes-AMG")
    ["points"] => Integer  516

我知道这很容易,但是解决此类问题的最快/最方便的方法是什么?

【问题讨论】:

    标签: php arrays multidimensional-array sum key


    【解决方案1】:

    应该像下面这样:

    function sum_teams($arr = []) {
        // array for final results
        $collection = [];
    
        foreach ($arr as $item) {
            $keys = array_keys($item);
    
            foreach ($keys as $key) {
                $value = $item[$key];
    
                // check if we captured the key already
                if ($collection[$key]) {
                    // increment up by current value
                    $collection[$key] += $value;
                } else {
                    // set to current value
                    $collection[$key] = $value;
                }
            }
        }
        return $collection;
    }
    // call it with your array
    $result = sum_teams($some_array);
    printr_r($result);
    
    

    【讨论】:

    • 工作正常,但我正在寻找更紧凑的解决方案。不过感谢您的帮助。
    【解决方案2】:

    您需要对结果进行迭代,在遇到新团队时向输出添加新条目,或者在再次找到同一团队时更新积分值。这最容易通过最初按团队名称索引输出,然后使用array_values 以数字方式重新索引数组来完成:

    $teams = array();
    foreach ($results as $result) {
        $team = $result['team'];
        if (!isset($teams[$team])) {
            $teams[$team] = array('team' => $team, 'points' => $result['punti']);
        }
        else {
            $teams[$team]['points'] += $result['punti'];
        }
    }
    $teams = array_values($teams);
    print_r($teams);
    

    输出(用于您的样本数据):

    Array
    (
        [0] => Array
            (
                [team] => Red Bull Racing
                [points] => 418
            )
        [1] => Array
            (
                [team] => Scuderia Ferrari
                [points] => 353
            )
        [2] => Array
            (
                [team] => Mercedes-AMG
                [points] => 516
            )
        [3] => Array
            (
                [team] => Racing Point F1
                [points] => 147
            )
        [4] => Array
            (
                [team] => Haas F1
                [points] => 127
            )
    )
    

    Demo on 3v4l.org

    【讨论】:

    • @GiovanniRizza 不用担心——我很高兴能帮上忙。
    猜你喜欢
    • 2013-02-24
    • 1970-01-01
    • 2023-02-11
    • 2016-08-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-12-29
    相关资源
    最近更新 更多