Array filter function in PHP - Time & Space Complexity
When we use the array filter function, we want to know how the time it takes changes as the array gets bigger.
We ask: How does filtering more items affect the work done?
Analyze the time complexity of the following code snippet.
$array = [1, 2, 3, 4, 5, 6];
$filtered = array_filter($array, fn($x) => $x % 2 === 0);
print_r($filtered);
This code filters an array to keep only even numbers.
- Primary operation: Checking each element with the callback function.
- How many times: Once for every element in the array.
As the array gets bigger, the number of checks grows in the same way.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 checks |
| 100 | 100 checks |
| 1000 | 1000 checks |
Pattern observation: The work grows directly with the number of items.
Time Complexity: O(n)
This means the time to filter grows in a straight line with the array size.
[X] Wrong: "Filtering an array is faster than checking every item."
[OK] Correct: The filter must look at each item to decide if it stays, so it takes time proportional to the array size.
Understanding how filtering scales helps you explain efficiency clearly and shows you know how common functions behave with bigger data.
"What if the callback function itself loops over another array for each element? How would the time complexity change?"
Practice
array_filter do to an array?Solution
Step 1: Understand the purpose of array_filter
The functionarray_filtertests each element with a callback function.Step 2: Determine what happens based on callback result
Elements for which the callback returns true are kept; others are removed.Final Answer:
It keeps only the elements where the callback returns true. -> Option BQuick Check:
array_filter keeps elements with true callback [OK]
- Thinking array_filter sorts the array
- Assuming it changes values to strings
- Believing it removes keys
array_filter with a callback function named isPositive on array $numbers?Solution
Step 1: Recall array_filter syntax
The second argument must be a callable, passed as a string or anonymous function.Step 2: Identify correct callable format
Passing the function name as a string, like 'isPositive', is correct.Final Answer:
array_filter($numbers, 'isPositive'); -> Option DQuick Check:
Callback function name as string [OK]
- Passing callback without quotes
- Swapping argument order
- Using & before function name
$arr = [1, 2, 3, 4, 5]; $result = array_filter($arr, fn($n) => $n % 2 === 0); print_r($result);
Solution
Step 1: Understand the callback function
The callback keeps numbers where $n % 2 === 0, meaning even numbers.Step 2: Identify even numbers and keys
Even numbers in the array are 2 and 4 at keys 1 and 3 respectively.Final Answer:
Array ( [1] => 2 [3] => 4 ) -> Option CQuick Check:
Only even numbers kept with original keys [OK]
- Assuming keys are reindexed
- Keeping odd numbers by mistake
- Confusing array keys with values
array_filter:$data = [10, 15, 20];
$result = array_filter($data, function($x) {
if ($x > 15) return true;
});
print_r($result);Solution
Step 1: Check callback return values
The callback returns true only if $x > 15, but returns nothing (null) otherwise.Step 2: Understand array_filter behavior
Callback must return true or false explicitly; null is treated as false but is unclear and risky.Final Answer:
The callback function does not return false for some values. -> Option AQuick Check:
Callback must return true or false explicitly [OK]
- Missing explicit false return
- Thinking array_filter needs 3 arguments
- Believing print_r can't print arrays
$items = ['apple' => 3, 'banana' => 0, 'cherry' => 5, 'date' => null];, which array_filter call will keep only items with values that are positive numbers (greater than zero)?Solution
Step 1: Understand the goal
We want to keep only values greater than zero (positive numbers).Step 2: Analyze each option
array_filter($items, fn($v) => $v > 0);keeps values where $v > 0, exactly what we want.array_filter($items, fn($v) => (bool)$v);keeps all truthy values, but 0 is false and null is false, so it removes them but also keeps negative numbers if any.array_filter($items);uses default filter which removes falsy values (0, null, false), but does not check for > 0.array_filter($items, fn($v) => $v !== 0);removes only zeros but keeps null and negative numbers.Final Answer:
array_filter($items, fn($v) => $v > 0); -> Option AQuick Check:
Filter with $v > 0 keeps only positive numbers [OK]
- Using default filter which keeps all truthy values
- Filtering only non-zero but allowing null
- Confusing truthy with positive
Ready to prove it?
Pass the test and this pattern moves unknown
