Bird
0
0

What is the output of the following PHP code?

medium📝 Predict Output Q4 of 15
PHP - Array Functions
What is the output of the following PHP code?
$numbers = [1, 2, 3, 4, 5];
$filtered = array_filter($numbers, fn($n) => $n > 3);
print_r($filtered);
AArray ( [1] => 2 [2] => 3 [3] => 4 )
BArray ( [3] => 4 [4] => 5 )
CArray ( [0] => 4 [1] => 5 )
DArray ( [0] => 1 [1] => 2 [2] => 3 )
Step-by-Step Solution
Solution:
  1. Step 1: Understand the callback condition

    The callback keeps numbers greater than 3, so 4 and 5 remain.
  2. Step 2: Check array keys in output

    array_filter preserves original keys, so keys 3 and 4 remain with values 4 and 5.
  3. Final Answer:

    Array ( [3] => 4 [4] => 5 ) -> Option B
  4. Quick Check:

    array_filter preserves keys = Array with keys 3 and 4 [OK]
Quick Trick: array_filter keeps original keys unless reindexed [OK]
Common Mistakes:
  • Assuming keys reset to 0
  • Including numbers <= 3
  • Misreading callback condition

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More PHP Quizzes