Home  >  Article  >  Backend Development  >  PHP array sum and average

PHP array sum and average

王林
王林Original
2023-05-11 09:13:36569browse

PHP is a well-known programming language that is ideal for web development. PHP has many useful functions, including functions for calculating the sum and average of arrays.

In this article, we will discuss how to calculate sums and averages using PHP arrays and provide some examples to give you a better understanding.

First, we need to create an array containing numerical values ​​so that we can perform calculations on them. The following is a simple array example:

$numbers = [10, 20, 30, 40, 50];

Using the array function array_sum() you can calculate the sum of an array. It takes an array as input and returns the sum of all elements in the array as shown below:

$total = array_sum($numbers); // 150

Using the same way, we can return the sum by simply array_sum() Calculate the average of an array by dividing the value by the number of elements in the array. The array function count() can count the number of elements in an array as follows:

$count = count($numbers); // 5
$average = $total / $count; // 30

Now, we have calculated the sum and average of the array. Here is the complete sample code:

$numbers = [10, 20, 30, 40, 50];

$total = array_sum($numbers); // 150
$count = count($numbers); // 5

$average = $total / $count; // 30

echo "数组总和为: $total<br>";
echo "数组平均值为: $average";

The output is as follows:

数组总和为: 150
数组平均值为: 30

If you want to do the same operation with an associative array, you can also use the function mentioned above. The difference is that array_sum() will iterate over all values ​​in the array, not just the numeric keys.

For example, consider the following associative array:

$book_prices = [
   "The Alchemist" => 15,
   "The Little Prince" => 12,
   "The Giver" => 10,
   "The Hobbit" => 18,
   "The Catcher in the Rye" => 14
];

If we want to calculate the sum and average of these book prices, we can do it like this:

$prices = array_values($book_prices);

$total = array_sum($prices); // 69
$count = count($prices); // 5

$average = $total / $count; // 13.8

echo "书的总共价格为: $total<br>";
echo "书的平均价格为: $average";

In the above code , we first store the prices in a simple array using the array_values() function. We then calculate the sum and average of this array, just like in the previous example.

Summary:

In PHP, calculating the sum and average of an array is very simple. Use the array_sum() function to calculate the sum of the array, use the count() function to get the number of elements in the array, and finally divide the two values ​​to get the average of the array. . Whether it is a numeric array or an associative array, you can easily calculate the sum and average of an array using the above method.

The above is the detailed content of PHP array sum and average. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn