Bird
Raised Fist0
PHPprogramming~20 mins

Array map function in PHP - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Array Map Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2: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);
?>
AArray ( [0] => 2 [1] => 4 [2] => 6 [3] => 8 )
BArray ( [1] => 2 [2] => 4 [3] => 6 [4] => 8 )
CArray ( [0] => 1 [1] => 2 [2] => 3 [3] => 4 )
DArray ( [0] => 1 [1] => 4 [2] => 9 [3] => 16 )
Attempts:
2 left
💡 Hint
array_map applies the function to each element and returns a new array with the results.
❓ Predict Output
intermediate
2: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);
?>
AArray ( [0] => 1 [1] => 2 [2] => 3 )
BArray ( [0] => 5 [1] => 7 [2] => 9 )
CArray ( [0] => 4 [1] => 5 [2] => 6 )
DArray ( [0] => 1 [1] => 7 [2] => 9 )
Attempts:
2 left
💡 Hint
array_map can take multiple arrays and pass corresponding elements to the callback.
🔧 Debug
advanced
2: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);
?>
AArray ( [0] => 1 [1] => 4 [2] => 9 )
BFatal error: Call to undefined function strtoupper()
CArray ( [0] => 1 [1] => 2 [2] => 3 )
DWarning: strtoupper() expects parameter 1 to be string, int given
Attempts:
2 left
💡 Hint
strtoupper expects strings, but the array contains integers.
🧠 Conceptual
advanced
2: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);
?>
AArray ( [0] => 11 [1] => 22 [2] => 3 [3] => 4 )
BArray ( [0] => 11 [1] => 22 )
CWarning: array_map(): Argument #2 is shorter than Argument #1
DArray ( [0] => 11 [1] => 22 [2] => 13 [3] => 24 )
Attempts:
2 left
💡 Hint
array_map uses NULL for missing elements in shorter arrays.
📝 Syntax
expert
2: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.
A
&lt;?php
$nums = [1, 2, 3];
$result = array_map(fn($n) =&gt; $n * 3, $nums);
print_r($result);
?&gt;
B
&lt;?php
$nums = [1, 2, 3];
$result = array_map(fn($n) =&gt; $n + 2, $nums);
print_r($result);
?&gt;
C
&lt;?php
$nums = [1, 2, 3];
$result = array_map(fn($n) =&gt; $n ** 2, $nums);
print_r($result);
?&gt;
D
&lt;?php
$nums = [1, 2, 3];
$result = array_map(fn($n) =&gt; $n - 1, $nums);
print_r($result);
?&gt;
Attempts:
2 left
💡 Hint
The output shows squares of the numbers.

Practice

(1/5)
1. What does the PHP function array_map do?
easy
A. Deletes elements from an array based on a condition
B. Merges two or more arrays into one
C. Sorts the elements of an array in ascending order
D. Applies a function to each element of an array and returns a new array

Solution

  1. Step 1: Understand the purpose of array_map

    The function applies a given callback function to each element of the input array.
  2. Step 2: Identify the output of array_map

    It returns a new array with the results of the callback function applied to each element.
  3. Final Answer:

    Applies a function to each element of an array and returns a new array -> Option D
  4. Quick Check:

    array_map = apply function to array elements [OK]
Hint: Remember: array_map transforms each item, returns new array [OK]
Common Mistakes:
  • Thinking array_map modifies the original array
  • Confusing array_map with array_filter
  • Assuming array_map merges arrays
2. Which of the following is the correct syntax to use array_map in PHP?
easy
A. array_map(function, array);
B. array_map(array, function);
C. array_map->function(array);
D. array_map(function => array);

Solution

  1. Step 1: Recall the function signature

    The correct syntax is array_map(callback_function, array_to_process).
  2. Step 2: Match the options with the syntax

    array_map(function, array); matches the correct order: function first, then array.
  3. Final Answer:

    array_map(function, array); -> Option A
  4. Quick Check:

    array_map(callback, array) syntax [OK]
Hint: Function comes first, array second in array_map [OK]
Common Mistakes:
  • Swapping the order of function and array
  • Using arrow or assignment syntax incorrectly
  • Using object method syntax instead of function call
3. What is the output of the following PHP code?
$numbers = [1, 2, 3];
$result = array_map(fn($n) => $n * 2, $numbers);
print_r($result);
medium
A. [2, 4, 6]
B. [1, 2, 3]
C. [3, 4, 5]
D. Error: Invalid callback

Solution

  1. Step 1: Understand the callback function

    The callback doubles each number: $n * 2.
  2. Step 2: Apply the callback to each element

    1*2=2, 2*2=4, 3*2=6, so the new array is [2, 4, 6].
  3. Final Answer:

    [2, 4, 6] -> Option A
  4. Quick Check:

    Each element doubled = [2, 4, 6] [OK]
Hint: Multiply each element by 2 in the callback [OK]
Common Mistakes:
  • Expecting original array unchanged
  • Confusing the callback operation
  • Thinking print_r outputs a string, not array
4. Identify the error in this PHP code using array_map:
$arr = [1, 2, 3];
$result = array_map('double', $arr);
function double($x) {
  return $x * 2;
}
print_r($result);
medium
A. array_map requires an anonymous function, not a named function
B. No error, code runs correctly
C. Function double is called before it is defined
D. Missing semicolon after array_map call

Solution

  1. Step 1: Check function definition and usage

    The function double is defined after the array_map call, but PHP allows this because functions are hoisted.
  2. Step 2: Verify syntax and function type

    array_map accepts named functions as callbacks, and semicolons are correctly placed.
  3. Final Answer:

    No error, code runs correctly -> Option B
  4. Quick Check:

    Named function callback works fine [OK]
Hint: PHP functions can be used before definition [OK]
Common Mistakes:
  • Assuming function must be defined before use
  • Thinking only anonymous functions work in array_map
  • Missing semicolon errors where none exist
5. Given an array of strings ['apple', 'banana', 'cherry'], how can you use array_map to create a new array with the length of each string?
hard
A. array_map(fn($s) => size($s), ['apple', 'banana', 'cherry']);
B. array_map(fn($s) => count($s), ['apple', 'banana', 'cherry']);
C. array_map(fn($s) => strlen($s), ['apple', 'banana', 'cherry']);
D. array_map(fn($s) => $s.length, ['apple', 'banana', 'cherry']);

Solution

  1. Step 1: Identify the correct function to get string length

    In PHP, strlen() returns the length of a string.
  2. Step 2: Apply strlen in array_map callback

    Using fn($s) => strlen($s) applies length calculation to each string.
  3. Final Answer:

    array_map(fn($s) => strlen($s), ['apple', 'banana', 'cherry']); -> Option C
  4. Quick Check:

    Use strlen() for string length in array_map [OK]
Hint: Use strlen() inside array_map callback for string lengths [OK]
Common Mistakes:
  • Using count() which counts array elements, not string length
  • Trying to access string length with dot notation
  • Using non-existent size() function
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard