Bird
0
0

You have an associative array with user IDs as keys and scores as values:

hard📝 Application Q15 of 15
PHP - Arrays
You have an associative array with user IDs as keys and scores as values:
$scores = [101 => 50, 102 => 75, 103 => 50, 104 => 90];
You want to sort the array by scores in descending order but keep the user IDs as keys. Which function should you use?
Aarsort()
Brsort()
Ckrsort()
Dsort()
Step-by-Step Solution
Solution:
  1. Step 1: Identify sorting by values descending with keys preserved

    arsort() sorts array by values in descending order and keeps keys intact.
  2. Step 2: Compare with other functions

    rsort() sorts values descending but resets keys, krsort() sorts keys descending, sort() sorts ascending and resets keys.
  3. Final Answer:

    arsort() -> Option A
  4. Quick Check:

    Sort values descending and keep keys = arsort() [OK]
Quick Trick: Use arsort() to sort values descending and keep keys [OK]
Common Mistakes:
  • Using rsort() which resets keys
  • Confusing krsort() with value sort
  • Using sort() which sorts ascending and resets keys

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More PHP Quizzes