It applies a user-defined function to every element of an array, allowing you to modify or use each element.
Click to reveal answer
beginner
How do you define the callback function used in array_walk()?
The callback function takes at least one parameter: the value of the current element, passed by reference if you want to modify it. It can also take a second optional parameter for the key.
Click to reveal answer
intermediate
What is the difference between array_walk() and array_map()?
array_walk() applies a function to each element and can modify the original array by reference. array_map() returns a new array with the results and does not modify the original array.
Click to reveal answer
beginner
Can array_walk() modify the original array elements?
Yes, if the callback function uses the value parameter by reference, it can change the original array elements.
Click to reveal answer
beginner
Write a simple example of array_walk() that appends '!' to each string in an array.
A. The array must be passed by reference to array_walk.
B. array_walk requires a named function, not an anonymous function.
C. Callback function parameter should be passed by reference to modify array.
D. print_r cannot print arrays modified by array_walk.
Solution
Step 1: Check how callback modifies array
The callback parameter $val is passed by value, so changes inside do not affect original array.
Step 2: Correct usage for modification
Passing &$val by reference allows modifying the original array elements.
Final Answer:
Callback function parameter should be passed by reference to modify array. -> Option C
Quick Check:
Use & in callback to change array items [OK]
Hint: Use & in callback parameter to modify array elements [OK]
Common Mistakes:
Ignoring reference in callback parameter
Thinking array_walk needs array by reference
Believing anonymous functions are not allowed
5. Given the array $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?
hard
A. array_walk($fruits, function($val) { if ($val < 4) $val += 10; });
B. array_walk($fruits, function(&$val) { if ($val < 4) $val += 10; });
C. array_walk($fruits, function(&$val, $key) { if ($key < 4) $val += 10; });
D. array_walk($fruits, function(&$val, $key) { if ($val > 4) $val += 10; });
Solution
Step 1: Identify condition and modification
We want to add 10 to values less than 4, so check $val < 4 inside the callback.
Step 2: Ensure modification affects original array
Pass $val by 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 B
Quick Check:
Reference + condition on value < 4 [OK]
Hint: Use & and if condition inside callback to selectively modify [OK]
Common Mistakes:
Not passing value by reference
Checking key instead of value
Using wrong comparison operator
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.