Bird
0
0

Which PHP function sorts an array and keeps the original keys intact?

easy📝 Conceptual Q11 of 15
PHP - Arrays
Which PHP function sorts an array and keeps the original keys intact?
A<code>rsort()</code>
B<code>sort()</code>
C<code>ksort()</code>
D<code>asort()</code>
Step-by-Step Solution
Solution:
  1. Step 1: Understand key preservation in sorting

    asort() sorts values but keeps the original keys unchanged.
  2. Step 2: Compare with other functions

    sort() resets keys, rsort() sorts in reverse but resets keys, ksort() sorts by keys, not values.
  3. Final Answer:

    asort() -> Option D
  4. Quick Check:

    Keep keys while sorting values = asort() [OK]
Quick Trick: Use asort() to sort values and keep keys unchanged [OK]
Common Mistakes:
  • Confusing sort() with asort() for key preservation
  • Thinking ksort() sorts by values
  • Assuming rsort() keeps keys

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More PHP Quizzes