Bird
0
0

Given this array:

hard📝 Application Q9 of 15
PHP - Arrays
Given this array:
$data = [5 => 'bird', 3 => 'cat', 7 => 'dog'];
You want to sort it by keys ascending but only keep keys greater than 4. Which code correctly does this?
Asort($data); $filtered = array_filter($data, fn($v, $k) => $k > 4, ARRAY_FILTER_USE_BOTH);
Barsort($data); $filtered = array_filter($data, fn($v, $k) => $k > 4, ARRAY_FILTER_USE_BOTH);
Csort($data); $filtered = array_filter($data, fn($v) => $v > 4);
D$filtered = array_filter($data, fn($v, $k) => $k > 4, ARRAY_FILTER_USE_BOTH); ksort($filtered);
Step-by-Step Solution
Solution:
  1. Step 1: Filter keys greater than 4 first

    Use array_filter with ARRAY_FILTER_USE_BOTH to filter by keys.
  2. Step 2: Sort filtered array by keys ascending

    Apply ksort() after filtering to sort keys ascending.
  3. Final Answer:

    $filtered = array_filter($data, fn($v, $k) => $k > 4, ARRAY_FILTER_USE_BOTH); ksort($filtered); -> Option D
  4. Quick Check:

    Filter keys first, then ksort() [OK]
Quick Trick: Filter keys first, then sort keys with ksort() [OK]
Common Mistakes:
  • Sorting before filtering
  • Filtering values instead of keys
  • Using sort() which resets keys

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More PHP Quizzes