- How to calculate the sum of values in an array in PHP?
- Example: PHP array_sum() function
- Example: Using for loop
- Conclusion
- array_sum
- Parameters
- Return Values
- Examples
- User Contributed Notes 6 notes
- How to Find the Sum of Numbers in Array Using PHP
- Method 1: Using array_sum() to Find Sum of Numbers in Array in PHP
- Method 2: Using Foreach Loop to Calculate Sum of Values in Array in PHP
How to calculate the sum of values in an array in PHP?
We can calculate the sum of array elements using the PHP predefined array_sum() function. This function takes the array name as its value and returns the sum of all the elements present within that particular array.
Example: PHP array_sum() function
In the given example, we have calculated the sum of all the array elements of the given array named $arr using the PHP predefined array_sum() function.
Sum of all the array elements is: 203
Apart from using a predefined PHP function, we can also calculate the sum of all the elements of an array using for loop.
Example: Using for loop
In the given example, we have calculated the sum of all the array elements of the given array $arr using for loop.
print("Sum of all the elements is: " . $total); ?>
Sum of all the array elements is: 203
Conclusion
In this lesson, we have learned how to calculate the sum of all the array elements in PHP. We discussed two methods. At first, we have calculated the sum of all the array elements using the array_sum() function, and second we used the for loop for calculating the sum of all the elements of an array.
array_sum
array_sum() returns the sum of values in an array.
Parameters
Return Values
Returns the sum of values as an integer or float; 0 if the array is empty.
Examples
Example #1 array_sum() examples
$a = array( 2 , 4 , 6 , 8 );
echo «sum(a) color: #007700″>. array_sum ( $a ) . «\n» ;
?php
$b = array( «a» => 1.2 , «b» => 2.3 , «c» => 3.4 );
echo «sum(b) color: #007700″>. array_sum ( $b ) . «\n» ;
?>
The above example will output:
User Contributed Notes 6 notes
If you want to calculate the sum in multi-dimensional arrays:
function array_multisum (array $arr ): float $sum = array_sum ( $arr );
foreach( $arr as $child ) $sum += is_array ( $child ) ? array_multisum ( $child ) : 0 ;
>
return $sum ;
>
?>
Example:
echo array_multisum ( $data );
Notably the function converts strings to float and ignores strings if they are not convertable:
$a = array( «String» , 2 , 4 , 6 , 8 );
echo «sum(a) keyword»>. array_sum ( $a ) . «\n» ;
$b = array( «12.3456» , 2 , 4 , 6 , 8 );
echo «sum(b) keyword»>. array_sum ( $b ) . «\n» ;
?>
sum(a) = 20
sum(b) = 32.3456
If you have a case where your array has int in strings, it sums them up as if there were only int in the array!
function sum_mix($a)
return array_sum($a);
>
var_dump(sum_mix([9, 3, ‘7’, ‘3’]));
Response will be int(22)
array_sum() doesn’t «ignore strings if they are not convertible», it converts them to zero. array_product() does the same thing, where the difference between «ignoring» and «converting to zero» is much more obvious.
//you can also sum multidimentional arrays like this;
?php>
function arraymultisum (array $arr ) $sum = null ;
foreach( $arr as $child ) $sum += is_array ( $child ) ? arraymultisum ( $child ): $child ;
>
return $sum ;
>
echo arraymultisum (array( 1 , 4 , 5 ,[ 1 , 5 , 8 ,[ 4 , 5 , 7 ]]));
array_sum converts strings to integer and array_sum(2,’2′) returns 4.
- Array Functions
- array_change_key_case
- array_chunk
- array_column
- array_combine
- array_count_values
- array_diff_assoc
- array_diff_key
- array_diff_uassoc
- array_diff_ukey
- array_diff
- array_fill_keys
- array_fill
- array_filter
- array_flip
- array_intersect_assoc
- array_intersect_key
- array_intersect_uassoc
- array_intersect_ukey
- array_intersect
- array_is_list
- array_key_exists
- array_key_first
- array_key_last
- array_keys
- array_map
- array_merge_recursive
- array_merge
- array_multisort
- array_pad
- array_pop
- array_product
- array_push
- array_rand
- array_reduce
- array_replace_recursive
- array_replace
- array_reverse
- array_search
- array_shift
- array_slice
- array_splice
- array_sum
- array_udiff_assoc
- array_udiff_uassoc
- array_udiff
- array_uintersect_assoc
- array_uintersect_uassoc
- array_uintersect
- array_unique
- array_unshift
- array_values
- array_walk_recursive
- array_walk
- array
- arsort
- asort
- compact
- count
- current
- end
- extract
- in_array
- key_exists
- key
- krsort
- ksort
- list
- natcasesort
- natsort
- next
- pos
- prev
- range
- reset
- rsort
- shuffle
- sizeof
- sort
- uasort
- uksort
- usort
- each
How to Find the Sum of Numbers in Array Using PHP
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.
The sum of numbers in the indexed array is: 60
The sum of numbers in the associative array is: 50The 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.