关联数组上的php array_sum

标签 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 )

谢谢!

最佳答案

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

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

关于关联数组上的php array_sum,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7604560/

相关文章:

javascript - PHP 如果基于输入信息的函数不起作用

php - 如何找到 PHPMyAdmin 数据库的正确详细信息?

php - "Notice: Undefined variable"、 "Notice: Undefined index"、 "Warning: Undefined array key"和 "Notice: Undefined offset"使用 PHP

ruby - Ruby 的新手,尝试将数组与嵌套数组进行比较

java - 如何将多维数组中的元素相加?

javascript - 大括号中的 PHP 动态 URL 变量

javascript - 无法使用 Node.js 处理 PHP 文件

c++ - C++中数组和字符串的索引

php - foreach $_POST 更新取决于 $key

php - 多维数组格式化