0
0
PHPprogramming~5 mins

Array map function in PHP - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
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?
AA callback function
BAn array to modify
CA string to append
DA number to multiply
What does array_map return?
AAn integer count
BThe original array unchanged
CA boolean value
DA new array with transformed elements
If you pass two arrays to array_map, how many arguments does the callback receive?
AOne argument
BTwo arguments
CZero arguments
DThree arguments
Which of these is a valid way to use array_map?
Aarray_map('strtoupper', ['a', 'b', 'c']);
Barray_map(['a', 'b', 'c'], 'strtoupper');
Carray_map('strtoupper');
Darray_map();
What happens if the arrays passed to array_map have different lengths?
AAn error is thrown
BThe callback runs until the longest array ends
CThe callback runs until the shortest array ends
DOnly the first array is processed
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.