Recall & Review
beginner
What does the
array_map function do in PHP?It applies a callback function to each element of one or more arrays and returns a new array with the modified elements.
Click to reveal answer
beginner
How do you pass a function to
array_map?You pass the function name as a string or an anonymous function as the first argument to
array_map.Click to reveal answer
intermediate
What happens if you pass multiple arrays to
array_map?The callback function receives elements from each array as arguments, and
array_map processes elements with the same keys together.Click to reveal answer
beginner
Example: What is the output of this code?<br>
$numbers = [1, 2, 3]; $result = array_map(fn($n) => $n * 2, $numbers); print_r($result);
The output is:<br>
Array
(
[0] => 2
[1] => 4
[2] => 6
)<br>Each number is doubled by the callback function.Click to reveal answer
intermediate
Why is
array_map useful compared to a foreach loop?array_map is more concise and expressive for transforming arrays, making code easier to read and maintain.Click to reveal answer
What is the first argument of
array_map in PHP?✗ Incorrect
The first argument must be a callback function that defines how to transform each element.
What does
array_map return?✗ Incorrect
array_map returns a new array with each element modified by the callback.If you pass two arrays to
array_map, how many arguments does the callback receive?✗ Incorrect
The callback receives one element from each array as separate arguments.
Which of these is a valid way to use
array_map?✗ Incorrect
The first argument is the callback function name, the second is the array.
What happens if the arrays passed to
array_map have different lengths?✗ Incorrect
Processing stops when the shortest array runs out of elements.
Explain how the
array_map function works in PHP and give a simple example.Think about how you change each item in a list using a function.
You got /4 concepts.
Describe what happens when you pass multiple arrays to
array_map and how the callback function handles them.Imagine pairing items from two lists and processing them together.
You got /3 concepts.