Implementation steps: 1. Use the array_sum() function to calculate the sum of all elements in the array, the syntax "$sum=array_sum($arr);"; 2. Use the count() function to calculate the length of the array, the syntax "$len=count($arr);"; 3. Use the "/" operator to divide the sum of elements by the array length to obtain the array average avg, the syntax is "$avg=$sum/$len;".
The operating environment of this tutorial: windows7 system, PHP version 8.1, DELL G3 computer
In php, you can use array_sum() and The count() function is used to find the average avg of an array.
Implementation steps:
Step 1. Use the array_sum() function to calculate the sum of all elements in the array
$sum=array_sum($arr);
Step 2: Use the count() function to calculate the length of the array (the number of elements in the array)
$len=count($arr);
Step 3: Use the "/" operator Divide the sum of elements by the length of the array to get the array average avg
$avg=$sum/$len;
Implementation example:
<?php header("content-type:text/html;charset=utf-8"); function f($arr){ $sum=array_sum($arr); echo "数组元素和:".$sum."<br>"; $len=count($arr); echo "数组长度:".$len."<br>"; $avg=$sum/$len; echo "数组平均值:".$avg."<br><br>"; } $arr=array(1,2,3,4,5,6,7,8,9,10); var_dump($arr); f($arr); $arr=array(2,4,6,8,10); var_dump($arr); f($arr); ?>
Function description
1. array_sum() function
array_sum() function can calculate all the values in the array The sum of the elements and returns the sum of the elements.
array_sum($array)
If all elements in
$array
are integers, return an integer value; if one or more of the values are floating point numbers, return a float Points.If there are elements of non-numeric type
in
$array, PHP will convert them into a numeric value (PHP is a weak language type and will be based on the value of the variable , automatically convert the variable to the correct data type), if the conversion fails, it will be used as a 0 value to participate in the calculation.
<?php header("Content-type:text/html;charset=utf-8"); $array= array("10.1xy", 100, '1', "0.01"); var_dump($array); echo '数组所有元素之和:'. array_sum($array); ?>
#2. count() function
The method to get the length of an array in PHP is very simple, PHP provides us with two functions to calculate the length of an array, namely the count() and sizeof() functions.
The count() function can count the number of all elements in the array, or the number of attributes in the object. Its syntax format is as follows:
count($array , $mode )
The parameter description is as follows:
- $array: is the array or object to be counted;
- $mode: is an optional parameter and can be omitted.
- If the $mode parameter is omitted, or set to COUNT_NORMAL or 0, the count() function will not detect multidimensional arrays;
- If $mode is set to COUNT_RECURSIVE or 1, the count() function Will recursively count the number of elements in the array, especially useful for counting the number of elements in multi-dimensional arrays.
Tip: If $array is neither an array nor an object, the count() function will return 1; if $array is equal to NULL, the count() function Return 0.
Example 1: Length of one-dimensional array
<?php header("content-type:text/html;charset=utf-8"); $arr=array(1,2,3,4,5,6,7,8,9); var_dump($arr); echo "数组长度为:".count($arr); ?>
Example 2: Length of two-dimensional array
<?php header("Content-type:text/html;charset=utf-8"); $arr= array ("张三", 25, array("高数","PHP教程","英语"), ); //输出语句 var_dump($arr); echo "数组长度为:".count($arr,1); ?>
sizeof() function is an alias of count() function, that is, the function and usage of sizeof() function are exactly the same as count() function.
Recommended learning: "PHP Video Tutorial"
The above is the detailed content of How to find the average value of an array avg in php. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

SecLists
SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment

MantisBT
Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

MinGW - Minimalist GNU for Windows
This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

SublimeText3 Linux new version
SublimeText3 Linux latest version
