- 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
- array_sum() function in PHP
- Syntax
- Parameters
- Return
- Example
- Output
- Example
- Output
- PHP array_sum() Function
- Definition and Usage
- Syntax
- Parameter Values
- Technical Details
- More Examples
- Example
- COLOR PICKER
- Report Error
- Thank You For Helping Us!
- PHP array_sum() Function
- Syntax
- Parameters
- Return value
- Example 1: How to Use array_sum() function in PHP
- Example 2: Using array_sum() function to calculate the average
- Example 3: Passing an empty array
- Example 4: Integers and String Values in array_sum()
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
array_sum() function in PHP
The array_sum() function returns the sum of the values in an array. The returned value can be integer or float. It returns 0 if the array is empty.
Syntax
Parameters
Return
The array_sum() function returns the sum of values. The returned value can be integer or float. It returns 0 if the array is empty.
Example
The following is an example −
Output
The following is the output −
Example
Let us see another example −
29.8,"b"=>66.4); echo array_sum($arr); ?>
Output
The following is the output −
I love programming (: That’s all I know
- Related Articles
- filter_has_var() function in PHP
- filter_id() function in PHP
- filter_input() function in PHP
- filter_input_array() function in PHP
- filter_list() function in PHP
- filter_var_array() function in PHP
- filter_var() function in PHP
- constant() function in PHP
- define() function in PHP
- defined() function in PHP
- die() function in PHP
- eval() function in PHP
- exit() function in PHP
- get_browser() function in PHP
- highlight_file() function in PHP
PHP array_sum() Function
Return the sum of all the values in the array (5+15+25):
Definition and Usage
The array_sum() function returns the sum of all the values in the array.
Syntax
Parameter Values
Technical Details
Return Value: Returns the sum of all the values in an array PHP Version: 4.0.4+ PHP Changelog: PHP versions prior to 4.2.1 modified the passed array itself and converted strings to numbers (which often converted them to zero, depending on their value) More Examples
Example
Return the sum of all the values in the array (52.2+13.7+0.9):
COLOR PICKER
Report Error
If you want to report an error, or if you want to make a suggestion, do not hesitate to send us an e-mail:
Thank You For Helping Us!
Your message has been sent to W3Schools.
Top Tutorials
Top References
Top Examples
Get Certified
W3Schools is optimized for learning and training. Examples might be simplified to improve reading and learning. Tutorials, references, and examples are constantly reviewed to avoid errors, but we cannot warrant full correctness of all content. While using W3Schools, you agree to have read and accepted our terms of use, cookie and privacy policy.
PHP array_sum() Function
The array_sum() function is “used to calculate the sum of all the values in an array(one-dimensional and associative).” It returns 0 if the array is empty. The returned sum may be an integer or float.
Syntax
Parameters
$array: It is an array whose sum needs to be calculated.
Return value
It returns the sum obtained after adding all the elements together.
Example 1: How to Use array_sum() function in PHP
$arr = [19, 21, 46]; echo array_sum($arr);
Example 2: Using array_sum() function to calculate the average
If you want to find an AVERAGE of the values in your array, use the sum and count the functions together. For example, let’s say your array is $arr, and you want the average.
$arr = [19, 21, 46]; $average = array_sum($arr) / count($arr); echo $average;
So, we can calculate the sum of values in an array using the array_sum() function.
Example 3: Passing an empty array
Example 4: Integers and String Values in array_sum()
If you pass the array containing both the strings and integers, the array_sum() Function returns a sum of integers. The strings are considered as 0.
$arr = ['krunal', 21, 'ankit', 19]; echo array_sum($arr);