multidimensional array array_sum

后端 未结 10 2014
深忆病人
深忆病人 2020-11-27 06:45

I have seen various posted about this question so am aware some answers to this may exist. however I am none the wiser after reading these.

I have an array that is

10条回答
  •  情歌与酒
    2020-11-27 07:04

    array_sum_multidimensional Helper function

    if (!function_exists('array_sum_multidimensional')) {
        /**
         * returns the sum of a element from an array of arrays
         */
    
        function array_sum_multidimensional(array $array, $elementKey){
    
            $sum = 0;
    
            foreach($array as $key => $value) {
                $sum += $value[ $elementKey ];
            }
    
            return $sum;
        }
    }
    

提交回复
热议问题