Home Backend Development PHP Problem PHP array sum and average

PHP array sum and average

May 11, 2023 am 09:13 AM

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];
Copy after login

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
Copy after login

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
Copy after login

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";
Copy after login

The output is as follows:

数组总和为: 150
数组平均值为: 30
Copy after login

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
];
Copy after login

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";
Copy after login

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 of this Website
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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

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

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)