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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
<?php //Declare indexed array of numbers $myarrayInd=array(13, 16, 31); //Find sum of numbers in indexed array using array_sum() $arrIndSum = array_sum($myarrayInd); //Print result echo "The sum of numbers in indexed array is: ".$arrIndSum; echo "<br>"; //Declare associative array of numbers $myarrayAssoc = array("a" => 21, "b" => 19, "c" => 10); //Find sum of numbers in associative array using array_sum() $arrAssocSum = array_sum($myarrayAssoc); //Print result echo "The sum of numbers in associative array is: ".$arrAssocSum; ?> |
Output
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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
<?php //Declare indexed array of numbers $myarrayInd=array(13, 16, 31); //Find sum of numbers in indexed array using array_sum() $sumIndValue = 0; foreach ($myarrayInd as $Indvalue) { $sumIndValue +=$Indvalue; } //Print result echo "The sum of numbers in indexed array is: ".$sumIndValue; echo "<br>"; //Declare associative array of numbers $myarrayAssoc = array("a" => 21, "b" => 19, "c" => 10); //Find sum of numbers in associative array using array_sum() $sumAssocValue = 0; foreach ($myarrayAssoc as $key=>$value) { $sumAssocValue +=$value; } //Print result echo "The sum of numbers in associative array is: ".$sumAssocValue; ?> |
Output
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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
<?php //Declare multidimensional array of numbers $mymultiarray = array( array("Cycle" => 2, "Bike" => 5, "Car" => 9), array("Cycle" => 3, "Bike" => 6, "Car" => 10), array("Cycle" => 5, "Bike" => 8, "Car" => 12) ); //Find sum of numbers in multidimensional array nested foreach loop $sumMulArray = 0; foreach ($mymultiarray as $subArray) { foreach ($subArray as $mulKey=>$mulValue) { $sumMulArray += $mulValue; } } //Print result echo "The sum of numbers in multidimensional array is: ".$sumMulArray; ?> |
Output
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