0
0
PHPprogramming~10 mins

Array reduce function in PHP - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Array reduce function
Start with array
Initialize accumulator with initial value
Take first element
Apply callback(accumulator, element)
Update accumulator with callback result
More elements?
YesTake next element
The reduce function starts with an initial value and applies a callback to combine each array element into a single result.
Execution Sample
PHP
<?php
$array = [1, 2, 3, 4];
$result = array_reduce($array, fn($acc, $item) => $acc + $item, 0);
echo $result;
?>
This code sums all numbers in the array using array_reduce starting from 0.
Execution Table
StepCurrent ElementAccumulator BeforeCallback ResultAccumulator AfterAction
1100 + 1 = 11Add first element to accumulator
2211 + 2 = 33Add second element to accumulator
3333 + 3 = 66Add third element to accumulator
4466 + 4 = 1010Add fourth element to accumulator
5-10-10No more elements, return accumulator
💡 All elements processed, final accumulator 10 returned
Variable Tracker
VariableStartAfter 1After 2After 3After 4Final
accumulator01361010
current element-1234-
Key Moments - 3 Insights
Why does the accumulator start with the initial value and not the first array element?
The accumulator starts with the initial value (0 here) so the callback always has a defined starting point. See execution_table row 1 where accumulator is 0 before adding the first element.
What happens if the array is empty?
If the array is empty, array_reduce returns the initial value immediately because there are no elements to process, as shown in the exit_note.
Why do we update the accumulator after each callback?
The accumulator holds the combined result so far. Each callback uses it to add the current element, then updates it for the next step, as shown in execution_table rows 1-4.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the accumulator value after processing the second element?
A2
B3
C1
D4
💡 Hint
Check the 'Accumulator After' column at Step 2 in execution_table
At which step does the accumulator become 6?
AStep 1
BStep 4
CStep 3
DStep 5
💡 Hint
Look at the 'Accumulator After' column in execution_table for value 6
If the initial value was 10 instead of 0, what would be the final accumulator?
A20
B10
C24
D30
💡 Hint
Sum array elements 1+2+3+4=10 plus initial 10 equals 20, check calculation carefully
Concept Snapshot
array_reduce(array, callback, initial) applies callback to combine array elements into one value.
Starts with initial accumulator value.
Callback receives accumulator and current element.
Returns final accumulator after processing all elements.
Useful for sums, products, or custom reductions.
Full Transcript
The PHP array_reduce function takes an array, a callback function, and an initial value. It starts with the initial value as the accumulator. Then for each element in the array, it calls the callback with the accumulator and the element. The callback returns a new accumulator value. This repeats until all elements are processed. Finally, array_reduce returns the accumulator. For example, summing [1,2,3,4] with initial 0 results in 10. If the array is empty, it returns the initial value immediately. The accumulator updates after each element to hold the combined result so far.