关联数组的php array_sum

时间:2011-09-29 23:43:14

标签: php arrays multidimensional-array

我有关联数组(动态 - 可能有更多数组,但键相同):

  Array
 (
    [food] => Array
      (
        [0] => 3
        [1] => 4
        [2] => 1
      )

     [liquor] => Array
       (
         [0] => 4
         [1] => 5
         [2] => 0
       )

     [beer] => Array
      (
        [0] => 5
        [1] => 6
        [2] => 0
      )

  )

我需要在每个数组上使用array_sum,因此结果为:

Array ( [food] => 8, [liquor] => 9, [beer] => 11 )

谢谢!

1 个答案:

答案 0 :(得分:7)

$result = array_map('array_sum', $your_array);

示例:http://ideone.com/LqTAV