Challenge - 5 Problems
Array Map Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of array_map with a simple callback
What is the output of the following PHP code?
PHP
<?php $numbers = [1, 2, 3, 4]; $result = array_map(fn($n) => $n * 2, $numbers); print_r($result); ?>
Attempts:
2 left
💡 Hint
array_map applies the function to each element and returns a new array with the results.
✗ Incorrect
The callback multiplies each number by 2, so the output array contains doubled values with the same keys starting from 0.
❓ Predict Output
intermediate2:00remaining
Using array_map with multiple arrays
What is the output of this PHP code?
PHP
<?php $a = [1, 2, 3]; $b = [4, 5, 6]; $result = array_map(fn($x, $y) => $x + $y, $a, $b); print_r($result); ?>
Attempts:
2 left
💡 Hint
array_map can take multiple arrays and pass corresponding elements to the callback.
✗ Incorrect
The callback adds elements from both arrays at the same positions: 1+4=5, 2+5=7, 3+6=9.
🔧 Debug
advanced2:00remaining
Identify the error in array_map usage
What error does this PHP code produce?
PHP
<?php $values = [1, 2, 3]; $result = array_map('strtoupper', $values); print_r($result); ?>
Attempts:
2 left
💡 Hint
strtoupper expects strings, but the array contains integers.
✗ Incorrect
strtoupper cannot convert integers to uppercase, so PHP warns about wrong argument type.
🧠 Conceptual
advanced2:00remaining
How does array_map handle arrays of different lengths?
Given these arrays, what will be the output of array_map?
PHP
<?php $a = [1, 2, 3, 4]; $b = [10, 20]; $result = array_map(fn($x, $y) => $x + $y, $a, $b); print_r($result); ?>
Attempts:
2 left
💡 Hint
array_map uses NULL for missing elements in shorter arrays.
✗ Incorrect
For missing elements in $b, NULL is passed, so adding NULL to int returns the int itself.
📝 Syntax
expert2:00remaining
Which option produces the output: Array ( [0] => 1 [1] => 4 [2] => 9 )?
Select the PHP code snippet that produces the output shown when run.
Attempts:
2 left
💡 Hint
The output shows squares of the numbers.
✗ Incorrect
Option C squares each number: 1^2=1, 2^2=4, 3^2=9.