draxu26480
2018-03-14 04:35
浏览 153
已采纳

php - 如何对多维数组求和

I have data below :

Array(
        [A] => Array
            (
                [AA] => 10
            )

        [B] => Array
            (
                [BA] => 5
                [BB] => 1
                [BC] => -2
            )

        [C] => Array
            (
                [CA] => 3
                [CB] => 0
            )
    )

I want to sum the value of second element my array (BA,BB,BC, etc) like this :

    Array(
        [A] => 10        
        [B] => 4
        [C] => 3
    )

I've tried to do with foreach (I'm using php as my platform) but the result is wrong, can someone give me explanation and the logic to solve this? thanks

图片转代码服务由CSDN问答提供 功能建议

我的数据如下:</ p>

  Array(
 [  A] =&gt;数组
(
 [AA] =&gt; 10 
)
 
 [B] =&gt;数组
(
 [BA] =&gt; 5 
 [BB] =  &gt; 1 
 [BC] =&gt; -2 
)
 
 [C] =&gt;数组
(
 [CA] =&gt; 3 
 [CB] =&gt; 0 
  )
)
 </ code> </ pre> 
 
 

我想将第二个元素的数值(BA,BB,BC等)的值加起来这样:</ p>

 数组(
 [A] =&gt; 10 
 [B] =&gt; 4 
 [C] =&gt; 3 
)
 </ code> </ pre>  
 
 

我尝试过使用foreach(我使用php作为我的平台),但结果是错误的,有人可以给我解释和逻辑来解决这个问题吗? 谢谢</ p> </ div>

3条回答 默认 最新

相关推荐 更多相似问题