Implementation steps: 1. Use array_sum() and " " operator to obtain the total element sum of three arrays, the syntax is "array_sum(array 1) array_sum(array 2) array_sum(array 3)"; 2. Use the count() function and the " " operator to get the total number of elements of the three arrays, the syntax is "count(array1) count(array2) count(array3)"; 3. Use the "/" operator to get the total average Number, syntax "total elements and/total number of elements".
The operating environment of this tutorial: windows7 system, PHP version 8.1, DELL G3 computer
In php, you can use array_sum(), The count() function and the " " and "/" operators are used to find the total average of three arrays.
Implementation idea:
First get the sum of the total elements of the three arrays
and then get The total number of elements of the three arrays
Finally, divide the sum of the total elements by the total number of elements to get the total average
Implementation steps:
Step 1: Use the array_sum() function and the " " operator to obtain the sum of the total elements of the three arrays
Use the array_sum() function to find the sum of the elements of the three arrays respectively.
Use " " to add the sum of the three elements to find the total sum of the elements.
<?php header("Content-type:text/html;charset=utf-8"); $arr1 = [1,2,3,4,5]; $arr2 = [2,3,6,7,8,9]; $arr3 = [10,12]; var_dump($arr1); echo "元素和:".array_sum($arr1); var_dump($arr2); echo "元素和:".array_sum($arr2); var_dump($arr3); echo "元素和:".array_sum($arr3); echo "<br><br>元素总和:".(array_sum($arr1)+array_sum($arr2)+array_sum($arr3)); ?>
Step 2: Use the count() function and the " " operator to obtain the total number of elements of the three arrays
Use the count() function to find the number of elements in the three arrays respectively
Use " " to add the three element numbers Find the total number of elements
<?php header("Content-type:text/html;charset=utf-8"); $arr1 = [1,2,3,4,5]; $arr2 = [2,3,6,7,8,9]; $arr3 = [10,12]; var_dump($arr1); echo "元素个数:".count($arr1); var_dump($arr2); echo "元素个数:".count($arr2); var_dump($arr3); echo "元素个数:".count($arr3); echo "<br><br>元素总个数:".(count($arr1)+count($arr2)+count($arr3)); ?>
Step 3: Use the "/" operator to get the total average
Use the "/" operator to divide the sum of the total elements by the total number of elements to obtain the total average. Syntax:
总元素和 / 总元素个数
Full sample code:
<?php header("Content-type:text/html;charset=utf-8"); $arr1 = [1,2,3,4,5]; $arr2 = [2,3,6,7,8,9]; $arr3 = [10,12]; var_dump($arr1); var_dump($arr1); var_dump($arr1); $sum=array_sum($arr1)+array_sum($arr2)+array_sum($arr3); echo "三个数组的总元素和:".$sum; $len=count($arr1)+count($arr2)+count($arr3); echo "<br>三个数组的总元素个数:".$len; $avg=$sum/$len; echo "<br>三个数组的总平均数:".$avg; ?>
Recommended learning: "PHP Video Tutorial"
The above is the detailed content of How to find the total average of three arrays in php. For more information, please follow other related articles on the PHP Chinese website!