The array walk function helps you change or use each item in a list one by one.
Array walk function in PHP
<?php function callback_function(&$value, $key, $user_data) { // Change or use $value here } array_walk(array &$array, callable $callback, mixed $user_data = null): bool; ?>
The callback function receives the value by reference, so you can change it directly.
The callback also gets the key and optional extra data you pass.
<?php function add_five(&$value, $key) { $value += 5; } $numbers = [1, 2, 3]; array_walk($numbers, 'add_five'); print_r($numbers);
<?php function greet($value, $key) { echo "Hello, $value!\n"; } $names = ['Anna', 'Bob']; array_walk($names, 'greet');
<?php function to_uppercase(&$value, $key) { $value = strtoupper($value); } $words = ['apple', 'banana']; array_walk($words, 'to_uppercase'); print_r($words);
<?php function add_suffix(&$value, $key, $suffix) { $value .= $suffix; } $fruits = ['apple', 'banana']; array_walk($fruits, 'add_suffix', ' fruit'); print_r($fruits);
This program adds an exclamation mark to each word in the list and shows the list before and after.
<?php function add_exclamation(&$value, $key) { $value .= '!'; } $words = ['hello', 'world', 'php']; // Before change print_r($words); // Change each word array_walk($words, 'add_exclamation'); // After change print_r($words); ?>
Time complexity is O(n) because it visits each item once.
Space complexity is O(1) since it changes items in place.
Common mistake: forgetting to pass the value by reference in the callback, so changes don't apply.
Use array_walk when you want to change or use each item without creating a new list. Use array_map if you want a new list instead.
array_walk lets you run a function on each item in a list.
You can change items directly by passing them by reference.
You can also pass extra data to the function if needed.
Practice
array_walk do?Solution
Step 1: Understand the purpose of array_walk
The functionarray_walkis designed to apply a callback function to each element of an array.Step 2: Compare with other array functions
Sorting, merging, or removing duplicates are done by other functions likesort,array_merge, orarray_unique.Final Answer:
It applies a user-defined function to every element of an array. -> Option AQuick Check:
array_walk = apply function to each element [OK]
- Confusing array_walk with sorting functions
- Thinking it merges arrays
- Assuming it removes duplicates
array_walk with a callback function named modify on an array $arr?Solution
Step 1: Recall array_walk syntax
The correct syntax isarray_walk(array, callback), where callback is a string with the function name.Step 2: Check the options
Only array_walk($arr, 'modify'); matches the correct order and uses quotes around the callback function name.Final Answer:
array_walk($arr, 'modify'); -> Option DQuick Check:
array_walk(array, 'function') syntax [OK]
- Swapping the order of parameters
- Omitting quotes around callback name
- Passing callback without quotes
$numbers = [1, 2, 3];
array_walk($numbers, function(&$value) {
$value *= 2;
});
print_r($numbers);Solution
Step 1: Understand the callback function
The callback multiplies each value by 2, and the value is passed by reference (&$value), so changes affect the original array.Step 2: Apply changes to each element
1*2=2, 2*2=4, 3*2=6, so the array becomes [2, 4, 6].Final Answer:
Array ( [0] => 2 [1] => 4 [2] => 6 ) -> Option AQuick Check:
Reference callback doubles values [OK]
- Not using & to modify original array
- Expecting original array unchanged
- Confusing output format
$arr = [1, 2, 3];
array_walk($arr, function($val) {
$val += 5;
});
print_r($arr);Solution
Step 1: Check how callback modifies array
The callback parameter$valis passed by value, so changes inside do not affect original array.Step 2: Correct usage for modification
Passing&$valby reference allows modifying the original array elements.Final Answer:
Callback function parameter should be passed by reference to modify array. -> Option CQuick Check:
Use & in callback to change array items [OK]
- Ignoring reference in callback parameter
- Thinking array_walk needs array by reference
- Believing anonymous functions are not allowed
$fruits = ['apple' => 2, 'banana' => 3, 'cherry' => 5];, how can you use array_walk to add 10 to each value only if the value is less than 4?Solution
Step 1: Identify condition and modification
We want to add 10 to values less than 4, so check$val < 4inside the callback.Step 2: Ensure modification affects original array
Pass$valby reference (&$val) to change original array values.Step 3: Verify options
array_walk($fruits, function(&$val) { if ($val < 4) $val += 10; }); correctly uses reference and condition. array_walk($fruits, function($val) { if ($val < 4) $val += 10; }); lacks reference, so no change. array_walk($fruits, function(&$val, $key) { if ($key < 4) $val += 10; }); checks key instead of value. array_walk($fruits, function(&$val, $key) { if ($val > 4) $val += 10; }); adds 10 when value is greater than 4, which is wrong.Final Answer:
array_walk($fruits, function(&$val) { if ($val < 4) $val += 10; }); -> Option BQuick Check:
Reference + condition on value < 4 [OK]
- Not passing value by reference
- Checking key instead of value
- Using wrong comparison operator
Ready to prove it?
Pass the test and this pattern moves unknown
