Home > Article > Backend Development > What are the functions for averaging arrays in php?
php array averaging functions are: 1. array_sum(), used to calculate the sum of all values in the array. In order to calculate the average, you can add all the values in the array and then divide by the array The number of elements; 2. array_reduce(), used to iterate the array and calculate each value with an initial value; 3. array_mean(), used to return the average of the array, first calculate the sum of the array, and calculate the array The number of elements. Divide the sum by the number of array elements to get the average.
The operating environment of this tutorial: Windows 10 system, php8.1.3 version, Dell g3 computer.
PHP is a scripting language commonly used for web development. In PHP, working with arrays is a common task. When working with arrays, you often need to calculate the average of the values in the array. In order to simplify the code and improve efficiency, PHP provides several functions to implement the average calculation of arrays. This article will introduce several commonly used PHP array averaging functions.
1. array_sum()
array_sum() function is used to calculate the sum of all values in an array. It takes an array as argument and returns the sum of all values in the array. To calculate the average, we can add all the values in the array and divide by the number of array elements.
The following is a sample code that uses the array_sum() function to calculate the average of an array:
$array = [1, 2, 3, 4, 5]; $sum = array_sum($array); $average = $sum / count($array); echo "数组的平均值为:" . $average;
The output result is: 3
2. array_reduce()
The array_reduce() function is used to iterate over the array and calculate each value with an initial value. It accepts two parameters: the array to iterate and a callback function. The callback function will accept two parameters: accumulated value and current value. The sum of an array can be calculated by summing the accumulated value and the current value in a callback function and returning the result. Then, divide the sum by the number of array elements to get the average of the array.
The following is a sample code that uses the array_reduce() function to calculate the average of an array:
$array = [1, 2, 3, 4, 5]; $average = array_reduce($array, function($carry, $item) { return $carry + $item; }) / count($array); echo "数组的平均值为:" . $average;
The output result is: 3
3. array_mean()
array_mean() function is a custom function used to return the average of an array. It accepts an array as a parameter and uses the array_sum() function to calculate the sum of the array and the count() function to calculate the number of array elements. Then, divide the sum by the number of array elements to get the average of the array.
The following is a sample code that uses the array_mean() function to calculate the average of an array:
function array_mean($array) { return array_sum($array) / count($array); } $array = [1, 2, 3, 4, 5]; $average = array_mean($array); echo "数组的平均值为:" . $average;
The output result is: 3
The above are several commonly used PHP array averages The function. Using these functions can simplify your code and make calculations more efficient. Choosing a function that suits you to calculate the average of an array can be decided based on the actual situation and personal preference. No matter which function you use, you can easily calculate the average of an array in PHP.
The above is the detailed content of What are the functions for averaging arrays in php?. For more information, please follow other related articles on the PHP Chinese website!