0
0
PHPprogramming~10 mins

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

Choose your learning style9 modes available
Concept Flow - Array walk function
Start with array
Pick first element
Apply callback function
Move to next element
More elements?
YesRepeat callback
No
End
The array walk function goes through each element of an array and applies a callback function to it one by one.
Execution Sample
PHP
<?php
$arr = [1, 2, 3];
array_walk($arr, function(&$v) { $v *= 2; });
print_r($arr);
?>
This code doubles each element in the array by walking through it and applying a function.
Execution Table
StepCurrent Element (value)Callback ActionArray State After Action
11Multiply by 2 → 2[2, 2, 3]
22Multiply by 2 → 4[2, 4, 3]
33Multiply by 2 → 6[2, 4, 6]
4No more elementsStop[2, 4, 6]
💡 All elements processed, array walk ends.
Variable Tracker
VariableStartAfter 1After 2After 3Final
arr[1, 2, 3][2, 2, 3][2, 4, 3][2, 4, 6][2, 4, 6]
v (current element)12466
Key Moments - 2 Insights
Why does the array change even though the callback uses a parameter?
Because the callback parameter is passed by reference (&$v), changes inside the callback affect the original array elements, as shown in steps 1-3 of the execution_table.
What happens if the callback does not modify the element?
The array stays the same because array_walk applies the callback but does not change elements unless the callback modifies them, as seen in the exit step where no changes occur.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the array state after step 2?
A[2, 2, 3]
B[2, 4, 3]
C[1, 2, 3]
D[2, 4, 6]
💡 Hint
Check the 'Array State After Action' column for step 2 in the execution_table.
At which step does the array element 3 become 6?
AStep 1
BStep 2
CStep 3
DStep 4
💡 Hint
Look at the 'Current Element' and 'Callback Action' columns in the execution_table.
If the callback function did not use & (reference), what would happen to the array?
AArray elements would remain unchanged
BArray elements would still be doubled
CArray would be emptied
DCallback would cause an error
💡 Hint
Refer to variable_tracker and key_moments about passing by reference.
Concept Snapshot
array_walk(array, callback) applies callback to each element.
Callback can modify elements if parameter is passed by reference (&).
Array changes happen inside callback if references used.
No return value; original array is modified directly.
Useful for simple element-wise operations.
Full Transcript
The array_walk function in PHP takes an array and a callback function. It goes through each element of the array one by one. For each element, it runs the callback function. If the callback changes the element by reference, the original array changes. In the example, each number in the array is doubled. The execution table shows each step: starting with 1, doubling it to 2, then 2 to 4, and 3 to 6. After all elements are processed, the function ends. This helps understand how array_walk modifies arrays by applying a function to each element.