0
0
PHPprogramming~5 mins

Array walk function in PHP - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
Recall & Review
beginner
What does the array_walk() function do in PHP?
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.
Example:<br><pre>function addExclamation(&$item, $key) {
  $item .= '!';
}
$array = ['Hi', 'Hello', 'Hey'];
array_walk($array, 'addExclamation');
// Result: ['Hi!', 'Hello!', 'Hey!']</pre>
Click to reveal answer
What parameters does the callback function for array_walk() receive?
AValue and key of each array element
BOnly the value of each element
COnly the key of each element
DThe entire array
Does array_walk() return a new array?
ANo, it returns the original array
BYes, always
CYes, but only if the callback returns a value
DNo, it returns true or false
How can you modify the original array elements inside the callback?
AYou cannot modify original elements
BBy passing the value parameter by reference
CBy using a global variable
DBy returning the new value from the callback
Which function is better to create a new array with modified values without changing the original array?
Aarray_walk()
Barray_filter()
Carray_map()
Darray_reduce()
What happens if the callback function in array_walk() does not accept the key parameter?
AThe key is ignored and only the value is passed
BThe function receives the key only
CIt causes an error
DThe array is not processed
Explain how array_walk() works and how it can modify an array.
Think about how you can change each item in a list by visiting them one by one.
You got /4 concepts.
    Compare array_walk() and array_map() in terms of modifying arrays.
    Consider whether you want to change the original list or create a new one.
    You got /4 concepts.