Backend Development 5 min read

Using PHP's array_sum() Function to Sum Array Elements

This article explains the built‑in PHP array_sum() function, demonstrates how to use it with both indexed and associative arrays, shows how to combine it with other functions like array_map() for more complex calculations, and highlights its performance and coding advantages.

php中文网 Courses
php中文网 Courses
php中文网 Courses
Using PHP's array_sum() Function to Sum Array Elements

The array_sum() function is a built‑in PHP function that calculates the sum of all elements in an array. It accepts an array as its argument and returns the total of its numeric values.

How to Use array_sum()

Below is a simple example that demonstrates how to use the array_sum() function to compute the sum of an array's elements:

$numbers = array(1, 2, 3, 4, 5);
$sum = array_sum($numbers);
echo "数组 $numbers 的总和是:$sum";

In this code, an array named $numbers containing five integers is defined, then array_sum() calculates the total, and the result is printed.

The array_sum() function can also be used with associative arrays; only the numeric values are summed, while string values are ignored. For example:

$prices = array('apple' => 0.99, 'banana' => 1.25, 'orange' => 0.75);
$total = array_sum($prices);
echo "最终总价是:$total";

Here, $prices is an associative array where the keys are fruit names and the values are their prices. Calling array_sum() returns the total price of all fruits.

Advantages of array_sum()

Using array_sum() allows developers to quickly and conveniently calculate the sum of an array's elements, regardless of the array size or value types. Because it eliminates the need to write explicit loops, the code becomes more concise and easier to maintain, and its performance is excellent for large data sets.

Beyond simple summation, array_sum() can be combined with other functions for more complex tasks. For instance, you can use array_sum() together with array_map() to sum corresponding elements of two arrays:

$numbers1 = array(1, 2, 3, 4, 5);
$numbers2 = array(6, 7, 8, 9, 10);

$sums = array_map(function($x, $y) {
    return $x + $y;
}, $numbers1, $numbers2);

$total = array_sum($sums);

echo "两个数组中元素之和的总和是:$total";

First, two arrays $numbers1 and $numbers2 are defined. array_map() adds each pair of elements, storing the results in $sums . Finally, array_sum() computes the sum of all values in $sums and displays the result.

Overall, the array_sum() function is a highly useful built‑in tool that can efficiently sum elements of any array, simplify code, improve maintainability, and be combined with other functions for advanced operations, making it valuable for everyday PHP development.

BackendPHPcode examplesFunctionsarray_sum
php中文网 Courses
Written by

php中文网 Courses

php中文网's platform for the latest courses and technical articles, helping PHP learners advance quickly.

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.