Skip to content

Core\Arr::average

Adds up all the values of an array and divides the total by how many entries there are. That is the mean.

An array with no entries gives you null. There is nothing to divide, so there is no mean.

A decimal anywhere in the array makes the result a decimal, which stays exact and is the type to use for money. Every other array gives you a float. A decimal result carries as many digits as the type holds, and the last digit is rounded.

The total is built the way Core\Arr::sum builds it. A total that grows past the range a whole number covers throws an error, and a float and a decimal in the same array throw an error too.

The examples show the mean of a list of whole numbers, an average price that stays exact, and the days a shop was busier than usual.

Parameters

  • $aarray<int|float|decimal>The numbers to average.

Return value

Returns ?float|decimal. A decimal when the total is a decimal, and a float otherwise; null for the empty array. A decimal result is exact, rounded half to even at the widest scale the quotient allows.

Errors

  • RuntimeErrorThe running total goes outside its type's range, or the array has both a float and a decimal, as in sum. The error is also thrown when a decimal quotient is outside the range of decimal.

Examples

The mean of a list of numbers

<?nvs
// Shows how to work out the mean of an array: the total of all values,
// divided by how many there are.
array<int> $goals = [3, 4, 5, 8];
// This prints 5. The keys are not used, only the values.
echo Core\Arr::average($goals), "\n";
// The result is a `float`, so a mean that is not a whole number keeps its
// fraction. This prints 4.5.
array<int> $points = [4, 5];
echo Core\Arr::average($points), "\n";
// An array with no entries has no mean, so the result is `null`. `??` gives
// you the text to show instead. This prints "no games yet".
array<int> $nothing = [];
echo Core\Arr::average($nothing) ?? 'no games yet', "\n";
Output
5
4.5
no games yet

An average price that stays exact

<?nvs
// Shows how to work out an average price. A `decimal` holds the value you
// wrote, so the mean is exact down to the last digit.
array<decimal> $prices = [19.99, 5.01, 0.10];
// One `decimal` in the array makes the result a `decimal`. This prints
// 8.366666666666666666666666667. The last digit is rounded.
echo Core\Arr::average($prices), "\n";
// The same three values as `float`. A `float` carries fewer digits, so this
// prints 8.3666666666667.
array<float> $inexact = [19.99, 5.01, 0.10];
echo Core\Arr::average($inexact), "\n";
Output
8.366666666666666666666666667
8.3666666666667

The days a shop was busier than usual

<?nvs
// Shows how to find the days a shop was busier than usual. The mean of the
// daily visitor counts is worked out first, and every day is compared to it.
array<int> $visitors = [
'mon' => 120,
'tue' => 95,
'wed' => 140,
'thu' => 88,
'fri' => 210,
'sat' => 260,
'sun' => 0,
];
// This prints "mean: 130.42857142857".
echo 'mean: ', Core\Arr::average($visitors), "\n";
float $mean = (Core\Arr::average($visitors) ?? 0.0) as float;
// This prints "wed 140", "fri 210" and "sat 260", one per line.
foreach ($visitors as string $day => int $count) {
if ($count > $mean) {
echo $day, ' ', $count, "\n";
}
}
Output
mean: 130.42857142857
wed 140
fri 210
sat 260