A. The callback function does not return false for some values.
B. array_filter requires a third argument for keys.
C. The array must be associative for array_filter to work.
D. The print_r function cannot print arrays.
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 A
Quick Check:
Callback must return true or false explicitly [OK]
Hint: Always return true or false in callback [OK]
Common Mistakes:
Missing explicit false return
Thinking array_filter needs 3 arguments
Believing print_r can't print arrays
5. Given an array $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)?
hard
A. array_filter($items, fn($v) => $v > 0);
B. array_filter($items, fn($v) => (bool)$v);
C. array_filter($items);
D. array_filter($items, fn($v) => $v !== 0);
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 A
Quick Check:
Filter with $v > 0 keeps only positive numbers [OK]
Hint: Use explicit condition for positive numbers [OK]
Common Mistakes:
Using default filter which keeps all truthy values
Filtering only non-zero but allowing null
Confusing truthy with positive
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.