How to Find the Sum of Numbers in Array Using PHP

Last Updated on June 23, 2021 by Roshan Parihar

In this tutorial, learn how to find the sum of numbers in an array in PHP. The short answer is to use the array_sum() that takes a single argument as the array variable.

You can calculate the sum of values present in the indexed array, associative array, and multidimensional. Let’s find out how to perform this task with the examples given below here.

Method 1: Using array_sum() to Find Sum of Numbers in Array in PHP

To get the sum of numbers or values in an array, you can use the array_sum(). The function is the built-in function of PHP that takes a single argument as the array variable. The array can be the indexed array or associative array.

It finds the values with each element of the array and calculates the sum of them automatically. Let’s find the sum of indexed as well as the associative array below.

Output

The sum of numbers in the indexed array is: 60
The sum of numbers in the associative array is: 50

The above example finds the sum of values present with each element of an array. The first result is the sum of numbers of indexed array. The second result is the calculation of the sum of numbers present in an associative array in PHP.

However, you cannot use the above function to get the sum of numbers or values in a multidimensional array. You can read further to learn the method to calculate the sum of numbers in a multidimensional array in PHP.

Method 2: Using Foreach Loop to Calculate Sum of Values in Array in PHP

In addition to the above method, you can also use the foreach loop of PHP to calculate the sum of values in an array using PHP. You have to first initialize a variable with zero (0). After that, inside the PHP foreach loop, you have to use the value of each element and the plus (+) operator to add them.

See the example given below that find the sum of values in an indexed array and associative array.

Output

The sum of Values in the indexed array is: 60
The sum of Values in the associative array is: 50

The above example gives the same result as the above example.

However, it takes some time to give the result of the addition. The array_sum() function gives the fast result in the output as compared to the foreach loop.

How to Calculate Addition of Values in Multidimensional Array in PHP

If you want to get the sum of values in a multidimensional array, you have to use the nested foreach loop of PHP. It requires initializing a variable with zero (0).

In each iteration inside the second foreach loop, you have to add the values of each element of an array in a multidimensional array in PHP.

Output

The addition of numbers in multidimensional array is: 60

The above output shows the result of the sum of values present in each element of the multidimensional array in PHP.

Do you have any other methods to get the sum of numbers in an array in PHP? If yes, please comment below. I will definitely add your methods in this post.

You May Also Like to Read

Leave a comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.