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

后端 未结 20 2512
花落未央
花落未央 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:21

    Here you have how I usually do this kind of operations.

    // We declare an empty array in wich we will store the results
    $sumArray = array();
    
    // We loop through all the key-value pairs in $myArray
    foreach ($myArray as $k=>$subArray) {
    
       // Each value is an array, we loop through it
       foreach ($subArray as $id=>$value) {
    
           // If $sumArray has not $id as key we initialize it to zero  
           if(!isset($sumArray[$id])){
               $sumArray[$id] = 0;
           }
    
           // If the array already has a key named $id, we increment its value
           $sumArray[$id]+=$value;
        }
     }
    
     print_r($sumArray);
    
    0 讨论(0)
  • 2020-11-22 01:23

    example array here

    Code here:

            $temp_arr = [];
            foreach ($a as $k => $v) {
                if(!is_null($v)) {
                    $sum = isset($temp_arr[$v[0]]) ? ((int)$v[5] + $sum) : (int)$v[5];
                    $temp_arr[$v[0]] = $sum;
                }
            }
            return $temp_arr;
    

    Result:

    {SEQ_OK: 1328,SEQ_ERROR: 561}
    
    0 讨论(0)
  • 2020-11-22 01:25

    Here is a solution similar to the two others:

    $acc = array_shift($arr);
    foreach ($arr as $val) {
        foreach ($val as $key => $val) {
            $acc[$key] += $val;
        }
    }
    

    But this doesn’t need to check if the array keys already exist and doesn’t throw notices neither.

    0 讨论(0)
  • 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());
    }
    
    0 讨论(0)
  • 2020-11-22 01:29
    $sumArray = array();
    foreach ($myArray as $k => $subArray) {
        foreach ($subArray as $id => $value) {
            if (!isset($sumArray[$id])) {
                $sumArray[$id] = 0;
            }
            $sumArray[$id]+=$value;
        }
    }
    
    0 讨论(0)
  • 2020-11-22 01:30
    $sumArray = array();
    
    foreach ($myArray as $k=>$subArray) {
      foreach ($subArray as $id=>$value) {
        if(!isset($sumArray[$id])){
         $sumArray[$id] =$value;
        }else {
         $sumArray[$id]+=$value;
        }
      }
    }
    
    print_r($sumArray);
    
    `
    
    0 讨论(0)
提交回复
热议问题