PHP Sum Of arrays inside a multidimensional array

前端 未结 2 661
感动是毒
感动是毒 2021-01-27 12:26

Need a little help here with summing up arrays inside a multi dimensional php array

Eg Multidimensional array

Array
(
    [0] => Array
        (
              


        
2条回答
  •  猫巷女王i
    2021-01-27 12:55

    This should work for arrays like the one of your example ($arr is an array like the one in your example, I haven't defined it here for simplicity's sake):

    $res = array();
    foreach($arr as $value) {
        foreach($value as $key => $number) {
            (!isset($res[$key])) ?
                $res[$key] = $number :
                $res[$key] += $number;
        }
    }
    
    print_r($res);
    

提交回复
热议问题