PHP array_sum() Function
In this chapter you will learn:
- Definition for PHP array_sum() Function
- Syntax for PHP array_sum() Function
- Parameter for PHP array_sum() Function
- Example - Sum an indexed array
- Example - Sum values in an associate array
Definition
The array_sum() function returns the sum of all the values in the array.
Syntax
PHP array_sum() Function has the following syntax.
array_sum(array)
Parameter
- array - Required. Array to sum
Example 1
Sum an indexed array
<?php
$a=array(5,15,25);
echo array_sum($a);
?>
The code above generates the following result.
Example 2
Return the sum of all the values in the array (2.2+1.7+0.9):
<?php
$a=array("a"=>2.2,"b"=>1.7,"c"=>0.9);
echo array_sum($a);
?>
The code above generates the following result.
Next chapter...
What you will learn in the next chapter:
- Definition for PHP array_udiff() Function
- Syntax for PHP array_udiff() Function
- Parameter for PHP array_udiff() Function
- Example - Compares the values of two arrays using a user-defined function, and returns the differences
- Example - Compares the values of three arrays using a user-defined function, and returns the differences
Home » PHP Tutorial » PHP Array Functions