In this code snippet, we’ll show an example of how to generate the mean average number from an array set of numbers in PHP.

Generate the mean average number from an array set of numbers in PHP

<?php
/**
 * Calculates the mean of an array of numbers.
 *
 * @param array $numbers_list The array of numbers.
 * @return float|null The mean of the numbers, or null if the array is empty or doesn't contain any valid numbers.
 */
function mean(array $numbers_list): ?float
{
    $valid_numbers = array_filter($numbers_list, 'is_numeric');

    if (empty($valid_numbers)) {
        return null;
    }

    return array_sum($valid_numbers) / count($valid_numbers);
}


echo mean([12, 145, 234, 345, 98]) . PHP_EOL;
echo mean([12, 33.6, 234, 'ABC', 98]) . PHP_EOL;
echo mean([]) . PHP_EOL;

MIT Licensed Code – See License

Tags: PHP, mean, array, array_sum, mean numbers, is_numeric, count, mean average, maths