Create an Array of Cumulative Sum in PHP
Last Updated :
05 Jun, 2024
The cumulative sum array is an array where each element is the sum of all previous elements in the original array up to the current index. This article explores different methods to create an array of cumulative sums in PHP.
What is a Cumulative Sum Array?
A cumulative sum array is an array where each element at index i is the sum of the elements from the original array up to index i. For example, given the array [1, 2, 3, 4], the cumulative sum array would be [1, 3, 6, 10].
Different approaches to creating an array of Cumulative Sum in PHP:
Using a for Loop
A basic method to create a cumulative sum array is to use a for loop to iterate through the original array and calculate the sum step by step.
- We initialize an empty array $cumulativeSumArr and a variable $sum to store the running total.
- We iterate through the original array using a for loop.
- During each iteration, we add the current element to $sum and append $sum to $cumulativeSumArr.
Example: This example shows the creation of an array of cumulative sum using for loop.
PHP
<?php
function cumulativeSum($arr) {
$cumulativeSumArr = [];
$sum = 0;
for ($i = 0; $i < count($arr); $i++) {
$sum += $arr[$i];
$cumulativeSumArr[] = $sum;
}
return $cumulativeSumArr;
}
// Driver code
$arr = [1, 2, 3, 4];
$res = cumulativeSum($arr);
print_r($res);
?>
OutputArray
(
[0] => 1
[1] => 3
[2] => 6
[3] => 10
)
Using array_reduce() Function
The array_reduce() function can be used to accumulate values, and we can use it to create a cumulative sum array by keeping track of the sums in an external variable.
- We initialize an empty array $cumulativeSumArr and a variable $sum to store the running total.
- We use array_reduce() function to iterate through the original array. The function updates the $sum and appends it to $cumulativeSumArr during each iteration.
Example: This example shows the creation of an array of cumulative sum using array_reduce() Function.
PHP
<?php
function cumulativeSum($arr) {
$cumulativeSumArr = [];
$sum = 0;
array_reduce($arr, function($carry, $item)
use (&$cumulativeSumArr, &$sum) {
$sum += $item;
$cumulativeSumArr[] = $sum;
return $carry;
});
return $cumulativeSumArr;
}
// Driver code
$arr = [1, 2, 3, 4];
$res = cumulativeSum($arr);
print_r($res);
?>
OutputArray
(
[0] => 1
[1] => 3
[2] => 6
[3] => 10
)
Using array_map() Function with Closure
Another approach is to use array_map() with a closure to create the cumulative sum array. This method is more functional and concise.
- We initialize a variable $sum to store the running total.
- We use array_map() function to apply a closure to each element in the original array. The closure updates $sum and returns it for each element, building the cumulative sum array.
Example: This example shows the creation of an array of cumulative sum using array_map() Function with Closure.
PHP
<?php
function cumulativeSum($arr) {
$sum = 0;
return array_map(function($item) use (&$sum) {
$sum += $item;
return $sum;
}, $arr);
}
// Driver code
$arr = [1, 2, 3, 4];
$res = cumulativeSum($arr);
print_r($res);
?>
OutputArray
(
[0] => 1
[1] => 3
[2] => 6
[3] => 10
)
Similar Reads
Creating a Zero-Filled Array in PHP Creating an array filled with zeros can be useful in various scenarios, such as initializing an array for further manipulation or setting up a default state. This article explores multiple approaches to creating a zero-filled array in PHP. A zero-filled array is an array where each element is initia
3 min read
Associative Arrays in PHP An associative array in PHP is a special array where each item has a name or label instead of just a number. Usually, arrays use numbers to find things. For example, the first item is at position 0, the second is 1, and so on. But in an associative array, we use words or names to find things. These
4 min read
PHP array_sum() Function The array_sum() function in PHP is used to calculate the sum of values in an array. It works with both indexed and associative arrays and can handle arrays containing both integers and floats. If the array is empty, it returns 0.number array_sum ( $array )Syntaxnumber array_sum(array $array)Paramete
2 min read
Return all dates between two dates in an array in PHP Returning all dates between two dates in an array means generating a list of all consecutive dates from the start date to the end date, inclusive, and storing each date as an element in an array for easy access.Here we have some common methods:Table of ContentUsing DatePeriod ClassUsing strtotime()
4 min read
Cumulative Sum in MATLAB The cumulative sum of a sequence is a running sums or partial sums of a sequence. The cumulative sums of the sequence {a,b,c,...}, are a, a+b, a+b+c, .... MATLAB allows us to calculate the cumulative sum of a vector, matrix using cumsum() method. Different syntax of cumsum() method are: B = cumsum(A
3 min read