Home > Article > Backend Development > PHP finds the average of three piles of arrays
In PHP programming, it is often necessary to operate on arrays, and finding the average of three piles of arrays is a common problem. This article will address this issue by introducing three different methods.
Method 1: Use for loop and array sum function
The most common method of averaging is to use a for loop to traverse the array, and use the array sum function to accumulate all elements of the array. Finally, divide by the number of elements to get the average value.
The sample code is as follows:
$arr1 = array(1, 2, 3, 4, 5); $arr2 = array(6, 7, 8, 9, 10); $arr3 = array(11, 12, 13, 14, 15); $sum1 = 0; $sum2 = 0; $sum3 = 0; $count = count($arr1); for ($i = 0; $i < $count; $i++) { $sum1 += $arr1[$i]; $sum2 += $arr2[$i]; $sum3 += $arr3[$i]; } $avg1 = $sum1 / $count; $avg2 = $sum2 / $count; $avg3 = $sum3 / $count; echo '第一堆平均数:' . $avg1 . '<br />'; echo '第二堆平均数:' . $avg2 . '<br />'; echo '第三堆平均数:' . $avg3 . '<br />';
Although this method is simple, it requires a for loop to traverse the array, which will affect the program efficiency when there are many array elements.
Method 2: Use the array_sum() and count() functions
PHP provides the array_sum() function to directly sum the arrays, and with the count function, you can quickly get the average value.
The sample code is as follows:
$arr1 = array(1, 2, 3, 4, 5); $arr2 = array(6, 7, 8, 9, 10); $arr3 = array(11, 12, 13, 14, 15); $sum1 = array_sum($arr1); $sum2 = array_sum($arr2); $sum3 = array_sum($arr3); $count = count($arr1); $avg1 = $sum1 / $count; $avg2 = $sum2 / $count; $avg3 = $sum3 / $count; echo '第一堆平均数:' . $avg1 . '<br />'; echo '第二堆平均数:' . $avg2 . '<br />'; echo '第三堆平均数:' . $avg3 . '<br />';
This method is simple and efficient, eliminating the need for a for loop to traverse the array.
Method 3: Use array_map() and array_reduce() functions
In addition to array_sum() and count() functions, PHP also provides array_map() and array_reduce() functions, which can be more Flexibly operate on arrays.
The sample code is as follows:
$arr1 = array(1, 2, 3, 4, 5); $arr2 = array(6, 7, 8, 9, 10); $arr3 = array(11, 12, 13, 14, 15); $sum1 = array_reduce($arr1, function($carry, $item) { return $carry + $item; }); $sum2 = array_reduce($arr2, function($carry, $item) { return $carry + $item; }); $sum3 = array_reduce($arr3, function($carry, $item) { return $carry + $item; }); $count = count($arr1); $avg1 = $sum1 / $count; $avg2 = $sum2 / $count; $avg3 = $sum3 / $count; echo '第一堆平均数:' . $avg1 . '<br />'; echo '第二堆平均数:' . $avg2 . '<br />'; echo '第三堆平均数:' . $avg3 . '<br />';
This method uses an anonymous function as the second parameter of array_reduce(), which allows for more flexible summation operations and avoids the use of for loops.
Summary
The three methods each have their own advantages and disadvantages, and the specific use needs to be selected according to the actual situation. Method 1 can be used for small-scale arrays, and method 2 or method 3 is recommended for larger-scale arrays. At the same time, it should be noted that when operating on an array, you need to pay attention to the type and number of array elements to avoid unexpected situations.
The above is the detailed content of PHP finds the average of three piles of arrays. For more information, please follow other related articles on the PHP Chinese website!