How to sum all column values in multi-dimensional array?

后端 未结 20 2511
花落未央
花落未央 2020-11-22 00:57

How can I add all the columnar values by associative key? Note that key sets are dynamic.

Input array:

Arr         


        
20条回答
  •  一生所求
    2020-11-22 01:26

    It can also be done using array_map :

    $rArray = array(
        0 => array(
            'gozhi' => 2,
            'uzorong' => 1,
            'ngangla' => 4,
            'langthel' => 5
        ),
        1 => array(
            'gozhi' => 5,
            'uzorong' => 0,
            'ngangla' => 3,
            'langthel' => 2
        ),
        2 => array(
            'gozhi' => 3,
            'uzorong' => 0,
            'ngangla' => 1,
            'langthel' => 3
        ),
    );
    
    $sumResult = call_user_func_array('array_map', array_merge(['sum'], $rArray));
    
    function sum()
    {
        return array_sum(func_get_args());
    }
    

提交回复
热议问题