Bird
0
0

Given $arr = ['x' => [1,2], 'y' => [3,4,5], 'z' => []];, which code correctly counts only the number of non-empty inner arrays?

hard📝 Application Q9 of 15
PHP - Arrays
Given $arr = ['x' => [1,2], 'y' => [3,4,5], 'z' => []];, which code correctly counts only the number of non-empty inner arrays?
Acount($arr)
Bcount($arr, COUNT_RECURSIVE)
Ccount(array_map('count', $arr))
Dcount(array_filter($arr))
Step-by-Step Solution
Solution:
  1. Step 1: Understand the array and goal

    The array has keys 'x', 'y', 'z' with inner arrays; 'z' is empty.
  2. Step 2: Use array_filter to remove empty arrays

    array_filter($arr) removes empty inner arrays, so only non-empty remain.
  3. Step 3: Count the filtered array

    Counting the filtered array gives the number of non-empty inner arrays.
  4. Final Answer:

    count(array_filter($arr)) -> Option D
  5. Quick Check:

    array_filter + count() counts non-empty arrays [OK]
Quick Trick: Use array_filter() to exclude empty arrays before counting [OK]
Common Mistakes:
  • Using COUNT_RECURSIVE which counts all elements
  • Counting original array without filtering
  • Using array_map('count') incorrectly

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More PHP Quizzes