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

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

提交回复
热议问题