在数组中添加数组中的结果

时间:2014-06-29 05:28:03

标签: php sum

我正在尝试将这两个数组中的gq_numplay添加到一起并回显总数。任何人都可以阐明我如何做到这一点吗?

感谢。

Array
(
    [1] => Array
    (
            [dedicated] => d
            [game_descr] => Counter-Strike: Global Offensive
            [game_dir] => csgo
            [gq_address] => xx.xx.xx.xx
            [gq_dedicated] => d
            [gq_gametype] => 
            [gq_hostname] => CS Server
            [gq_joinlink] => steam://connect/
            [gq_mapname] => de_dust2
            [gq_maxplayers] => 24
            [gq_mod] => csgo
            [gq_numplayers] => 1
            [gq_online] => 1
            [gq_password] => 0
            [gq_port] => 27017
            [gq_protocol] => source
            [gq_transport] => udp
            [gq_type] => csgo
            [hostname] => CS Server
            [map] => de_dust2
            [max_players] => 24
            [num_bots] => 0
            [num_players] => 1
        )

    [2] => Array
        (
            [cd_announcer_version] => 2.3
            [coop] => 0
            [deathmatch] => 1
            [decalfrequency] => 15
            [dedicated] => d
            [game_descr] => Team Fortress
            [game_dir] => tf
            [gq_address] => xx.xx.xx.xx
            [gq_dedicated] => d
            [gq_gametype] => 
            [gq_hostname] => TF2 Server
            [gq_joinlink] => steam://connect/
            [gq_mapname] => mvm_rottenburg
            [gq_maxplayers] => 6
            [gq_mod] => tf
            [gq_numplayers] => 0
            [gq_online] => 1
            [gq_password] => 0
            [gq_port] => 27019
            [gq_protocol] => source
            [gq_transport] => udp
        )
)

我已经能够从每个数组中提取单个值,但我不能将它们加在一起:(

2 个答案:

答案 0 :(得分:0)

你错过了那里被转储的变量的名称,但是假设它是$ foo,试试

echo $foo[1]['gq_numplayers'] + $foo[2]['gq_numplayers'];

答案 1 :(得分:0)

使用foreach循环遍历每个元素:

$total = 0;
foreach($arr as $x) {
  $total += $x['gq_numplayers'];
}

echo $total;