Bird
0
0

Identify the error in this PHP code snippet:

medium📝 Debug Q6 of 15
PHP - Arrays
Identify the error in this PHP code snippet:
$arr = [3 => 'x', 1 => 'y', 2 => 'z'];
arsort($arr, SORT_NUMERIC);
print_r($arr);
ASORT_NUMERIC is invalid with arsort()
Barsort() does not preserve keys
CNo error; code runs correctly
DArray keys must be numeric for arsort()
Step-by-Step Solution
Solution:
  1. Step 1: Check arsort() with SORT_NUMERIC

    arsort() accepts sorting flags like SORT_NUMERIC to sort values numerically descending.
  2. Step 2: Verify key preservation and syntax

    arsort() preserves keys; code syntax is correct and runs without error.
  3. Final Answer:

    No error; code runs correctly -> Option C
  4. Quick Check:

    arsort() supports SORT_NUMERIC and preserves keys [OK]
Quick Trick: arsort() supports sorting flags and preserves keys [OK]
Common Mistakes:
  • Thinking SORT_NUMERIC invalid
  • Expecting keys to reset
  • Believing keys must be numeric

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More PHP Quizzes