Array map function in PHP - Time & Space Complexity
We want to understand how the time needed to run an array map function changes as the array gets bigger.
Specifically, how does the work grow when we apply a function to each item in an array?
Analyze the time complexity of the following code snippet.
$array = [1, 2, 3, 4, 5];
$result = array_map(function($item) {
return $item * 2;
}, $array);
This code takes each number in the array and doubles it, creating a new array with the results.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Applying the function to each element in the array.
- How many times: Once for every element in the array.
As the array gets bigger, the number of times we apply the function grows directly with the number of items.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 function calls |
| 100 | 100 function calls |
| 1000 | 1000 function calls |
Pattern observation: The work grows in a straight line with the size of the array.
Time Complexity: O(n)
This means the time to finish grows directly with the number of items in the array.
[X] Wrong: "The array_map function runs in constant time no matter the array size."
[OK] Correct: The function must visit each item to apply the callback, so time grows with array size.
Knowing how array_map scales helps you explain how your code handles bigger data, a skill interviewers appreciate.
"What if the callback function inside array_map itself contains a loop? How would the time complexity change?"
Practice
array_map do?Solution
Step 1: Understand the purpose of array_map
The function applies a given callback function to each element of the input array.Step 2: Identify the output of array_map
It returns a new array with the results of the callback function applied to each element.Final Answer:
Applies a function to each element of an array and returns a new array -> Option DQuick Check:
array_map = apply function to array elements [OK]
- Thinking array_map modifies the original array
- Confusing array_map with array_filter
- Assuming array_map merges arrays
array_map in PHP?Solution
Step 1: Recall the function signature
The correct syntax is array_map(callback_function, array_to_process).Step 2: Match the options with the syntax
array_map(function, array); matches the correct order: function first, then array.Final Answer:
array_map(function, array); -> Option AQuick Check:
array_map(callback, array) syntax [OK]
- Swapping the order of function and array
- Using arrow or assignment syntax incorrectly
- Using object method syntax instead of function call
$numbers = [1, 2, 3]; $result = array_map(fn($n) => $n * 2, $numbers); print_r($result);
Solution
Step 1: Understand the callback function
The callback doubles each number: $n * 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].Final Answer:
[2, 4, 6] -> Option AQuick Check:
Each element doubled = [2, 4, 6] [OK]
- Expecting original array unchanged
- Confusing the callback operation
- Thinking print_r outputs a string, not array
array_map:$arr = [1, 2, 3];
$result = array_map('double', $arr);
function double($x) {
return $x * 2;
}
print_r($result);Solution
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.Step 2: Verify syntax and function type
array_map accepts named functions as callbacks, and semicolons are correctly placed.Final Answer:
No error, code runs correctly -> Option BQuick Check:
Named function callback works fine [OK]
- Assuming function must be defined before use
- Thinking only anonymous functions work in array_map
- Missing semicolon errors where none exist
['apple', 'banana', 'cherry'], how can you use array_map to create a new array with the length of each string?Solution
Step 1: Identify the correct function to get string length
In PHP,strlen()returns the length of a string.Step 2: Apply strlen in array_map callback
Usingfn($s) => strlen($s)applies length calculation to each string.Final Answer:
array_map(fn($s) => strlen($s), ['apple', 'banana', 'cherry']); -> Option CQuick Check:
Use strlen() for string length in array_map [OK]
- Using count() which counts array elements, not string length
- Trying to access string length with dot notation
- Using non-existent size() function
Ready to prove it?
Pass the test and this pattern moves unknown
