如何计算多数组中的和时的平均值

时间:2022-03-02 21:44:16

i have already function that SUM sub arrays which has matches keys like this

我已经运行了SUM子数组,它匹配了这样的键

 $totals = array();
 $count_loops = 0 ; 
    // First get our totals.
        foreach ($average as $subKey => $subArray) {
            foreach ($subArray as $k => $v) {
                // Add the column to our total.
                $totals[$k] = isset($totals[$k] ) ? $totals[$k] + $v / $count_loops : $v;
            }
        }

and return total array like this

并返回像这样的总数组

Array
(
    [john_total] => 519.44
    [adam_total] => 1664.64
    [sara_total] => 1237.53
}

but i want to return average after sum bu divide sum number with count loops,but it return wrong average..if i delete this code / $count_loops it sum good..so how to do this

但是我希望在总和数除以和计数循环之后返回平均值,但它返回错误的平均值..如果我删除此代码/ $ count_loops它总和好..所以如何做到这一点

3 个解决方案

#1


Think you can use : array_sum and count function in PHP :

认为你可以在PHP中使用:array_sum和count函数:

$totals = array();
 $count_loops = 0 ; 
    // First get our totals.
        foreach ($average as $subKey => $subArray) {
            $totals[$k] = array_sum($subArray) / count($subArray);
        }

#2


By using array_sum and count

通过使用array_sum和count

array_sum will give you sum of total result.

array_sum将给出总结果的总和。

and count will give you total number of element.

和count将给你元素的总数。

and average is A calculated "central" value of a set of numbers.

和平均值是一组数字的计算“中心”值。

so

$average = array_sum($array) / count($array);

#3


Be mindful of the calculation which is going to happen at this point of your code.

请注意在代码的这一点上将要进行的计算。

$totals[$k] = isset($totals[$k] ) ? $totals[$k] + $v / $count_loops : $v;

I'd rather you have it as

我宁愿你拥有它

$totals[$k] = isset($totals[$k] ) ? ( $totals[$k] + $v ) / $count_loops : $v;

( 5 + 2.5 ) / 2 => 7.5 / 2 = 3.75

(5 + 2.5)/ 2 => 7.5 / 2 = 3.75

5 + ( 2.5 / 2 ) => 5 + 1.25 = 6.25

5 +(2.5 / 2)=> 5 + 1.25 = 6.25

Those calculations are same figures but dependent on operator precedence. See http://php.net/manual/en/language.operators.precedence.php

这些计算是相同的数字,但取决于运算符优先级。请参见http://php.net/manual/en/language.operators.precedence.php

#1


Think you can use : array_sum and count function in PHP :

认为你可以在PHP中使用:array_sum和count函数:

$totals = array();
 $count_loops = 0 ; 
    // First get our totals.
        foreach ($average as $subKey => $subArray) {
            $totals[$k] = array_sum($subArray) / count($subArray);
        }

#2


By using array_sum and count

通过使用array_sum和count

array_sum will give you sum of total result.

array_sum将给出总结果的总和。

and count will give you total number of element.

和count将给你元素的总数。

and average is A calculated "central" value of a set of numbers.

和平均值是一组数字的计算“中心”值。

so

$average = array_sum($array) / count($array);

#3


Be mindful of the calculation which is going to happen at this point of your code.

请注意在代码的这一点上将要进行的计算。

$totals[$k] = isset($totals[$k] ) ? $totals[$k] + $v / $count_loops : $v;

I'd rather you have it as

我宁愿你拥有它

$totals[$k] = isset($totals[$k] ) ? ( $totals[$k] + $v ) / $count_loops : $v;

( 5 + 2.5 ) / 2 => 7.5 / 2 = 3.75

(5 + 2.5)/ 2 => 7.5 / 2 = 3.75

5 + ( 2.5 / 2 ) => 5 + 1.25 = 6.25

5 +(2.5 / 2)=> 5 + 1.25 = 6.25

Those calculations are same figures but dependent on operator precedence. See http://php.net/manual/en/language.operators.precedence.php

这些计算是相同的数字,但取决于运算符优先级。请参见http://php.net/manual/en/language.operators.precedence.php