Array Functions
Advertisements

PHP array_sum() Function

Topic: PHP Array ReferencePrev|Next

Description

The array_sum() function calculates the sum of all the values in an array.

The following table summarizes the technical details of this function.

Return Value: Returns the sum of all the values in an array as an integer or float; 0 if the array is empty.
Version: PHP 4.0.4+

Syntax

The basic syntax of the array_sum() function is given with:

array_sum(array);

The following example shows the array_sum() function in action.

<?php
// Sample array
$numbers = array(1, 2, 5, 7, 10);

// Getting the sum of array values
echo array_sum($numbers); // Prints: 25
?>

Parameters

The array_sum() function accepts the following parameters.

Parameter Description
array Required. Specifies the array to work on.

More Examples

Here're some more examples showing how array_sum() function actually works:

You can also use this function with associative arrays having numeric values, as shown here:

<?php
// Sample array
$persons = array("Harry"=>18, "Clark"=>32, "John"=>24);

// Getting the sum of array values
echo array_sum($persons); // Prints: 74
?>
Advertisements
Bootstrap UI Design Templates

Your Feedback:

We would love to hear from you, please drop us a line.