How to Calculate the Sum of Array Elements in PHP ?

Given an integer array arr of size n, find the sum of all its elements in PHP. 

Example: Consider the below examples:

Input : arr[5] = [ 1, 2, 3, 6, 5 ]
Output : 17
Explanation : 1 + 2 + 3 + 6 + 5 = 17

Input : arr[] = [ 15, 12, 13, 10 ]
Output : 50

There are different methods to calculate the sum of array values:

Table of Content

  • Using array_sum() Function
  • Using for Loop
  • Using array_reduce() Function
  • Using Recursion
  • Using foreach Loop

Using array_sum() Function

The array_sum() function returns the sum of all the values in an array (one-dimensional and associative). It takes an array parameter and returns the sum of all the values in it.

Syntax

int|float array_sum( array $array );

Example: PHP Program to get the sum of all the Array Elements using the array_sum() Function.

PHP
<?php

// Array with values
$arr = [21, 12, 16, 14, 18, 22];

// Calculating sum of array elements
$sumofelements = array_sum($arr);

print_r($sumofelements);

?>

Output
103

Using for Loop

We can use for loop to calculate the sum of array elements. First, we declare a variable and initialize with zero and then loop over array elements and sum up each array element in the declared variable.

Syntax

$sum = 0;
for ( $i = 0; $i < sizeof($arr); $i++ ) {
$sum = $sum + $arr[i];
}

Example: PHP Program to get the sum of all the Array Elements using For Loop.

PHP
<?php

$arr = [21, 12, 16, 14, 18, 22];

// Initialize sum with 0
$sum = 0;

// Loop through the array and add
// each element to the sum
for ($i = 0; $i < sizeof($arr); $i++) {
    $sum = $sum + $arr[$i];
}

// Display the result
echo "Sum of Array Values:" . $sum;

?>

Output
Sum of Array Values:103

Using array_reduce() Function

The array_reduce() function is used to reduce the array elements into a single value that can be of float, integer or string. The function uses a user-defined callback function to reduce the input array.

Syntax

mixed array_reduce
(
array $array,
callable $callback,
mixed $initial = null
);

Example: PHP Program to get the sum of all the Array Elements using the array_reduce() Function.

PHP
<?php

$arr = [21, 12, 16, 14, 18, 22];

// Use array_reduce to calculate the sum
$sum = array_reduce(
    $arr,
    function ($carry, $elm) {
        return $carry + $elm;
    },
    0
);

// Display the result
echo "Sum of Array Values:" . $sum;

?>

Output
Sum of Array Values:103

Using Recursion

We can use recursion to calculate the sum of array values. The recursive calculates the sum of an array by breaking it down into two cases: the base case, where if the array is empty the sum is 0; and the recursive case, where the sum is calculated by adding the first element to the sum of the remaining elements which is computed through a recursive call with the array shifted by one position and size reduced by one.

Example: PHP Program to get the sum of all the Array Elements using Recursion.

PHP
<?php

function sumofElements($arr, $n)
{
    // Base or terminating condition
    if ($n <= 0) {
        return 0;
    }
  
    // Calling method recursively
    return sumofElements($arr, $n - 1) + $arr[$n - 1];
}

$arr = [21, 12, 16, 14, 18, 22];

$sum = sumofElements($arr, sizeof($arr));

// Display the result
echo "Sum of Array Values:" . $sum;

?>

Output
Sum of Array Values:103

Using foreach Loop

To calculate the sum of array elements in PHP using a `foreach` loop, initialize a sum variable to 0, then iterate through each element of the array, adding its value to the sum. Finally, echo or return the sum.

Example: PHP script sums elements of an array `[1, 2, 3, 4, 5]`, outputs result (`15`) to console using `fwrite(STDOUT, $sum . PHP_EOL);`.

PHP
<?php

$array = [1, 2, 3, 4, 5];
$sum = 0;

foreach ($array as $value) {
    $sum += $value;
}

// Output the sum to console
fwrite(STDOUT, $sum . PHP_EOL);

?>

Output
15