Bird
Raised Fist0
PHPprogramming~20 mins

Array sort functions in PHP - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Array Sort Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
What is the output of this PHP code using sort()?
Consider the following PHP code snippet. What will be the output after sorting the array with sort()?
PHP
<?php
$arr = [3, 1, 4, 1, 5];
sort($arr);
print_r($arr);
?>
AArray ( [0] => 1 [1] => 3 [2] => 4 [3] => 5 [4] => 1 )
BArray ( [0] => 1 [1] => 1 [2] => 3 [3] => 4 [4] => 5 )
CArray ( [0] => 3 [1] => 1 [2] => 4 [3] => 1 [4] => 5 )
DArray ( [0] => 5 [1] => 4 [2] => 3 [3] => 1 [4] => 1 )
Attempts:
2 left
💡 Hint
sort() sorts the array in ascending order and reindexes the keys.
❓ Predict Output
intermediate
2:00remaining
What is the output of this PHP code using asort()?
Look at this PHP code. What will be printed after using asort() on the array?
PHP
<?php
$arr = [3 => 'apple', 1 => 'banana', 2 => 'cherry'];
asort($arr);
print_r($arr);
?>
AArray ( [3] => apple [2] => cherry [1] => banana )
BArray ( [1] => banana [3] => apple [2] => cherry )
CArray ( [3] => apple [1] => banana [2] => cherry )
DArray ( [2] => cherry [1] => banana [3] => apple )
Attempts:
2 left
💡 Hint
asort() sorts values but keeps original keys.
❓ Predict Output
advanced
2:00remaining
What is the output of this PHP code using krsort()?
What will be the output after sorting the array by keys in descending order using krsort()?
PHP
<?php
$arr = ['a' => 3, 'c' => 1, 'b' => 2];
krsort($arr);
print_r($arr);
?>
AArray ( [b] => 2 [c] => 1 [a] => 3 )
BArray ( [a] => 3 [b] => 2 [c] => 1 )
CArray ( [c] => 3 [b] => 2 [a] => 1 )
DArray ( [c] => 1 [b] => 2 [a] => 3 )
Attempts:
2 left
💡 Hint
krsort() sorts keys in reverse (descending) order.
❓ Predict Output
advanced
2:00remaining
What error does this PHP code raise when using usort() incorrectly?
What error will this PHP code produce?
PHP
<?php
$arr = [3, 1, 4];
usort($arr, 'nonexistent_function');
print_r($arr);
?>
AWarning: usort() expects parameter 2 to be a valid callback, function 'nonexistent_function' not found
BFatal error: Call to undefined function nonexistent_function()
CArray ( [0] => 1 [1] => 3 [2] => 4 )
DParse error: syntax error, unexpected 'usort'
Attempts:
2 left
💡 Hint
usort() requires a valid callback function name as second argument.
🧠 Conceptual
expert
2:00remaining
How many items are in the array after this PHP code runs?
Given this PHP code, how many items will the array contain after sorting?
PHP
<?php
$arr = [0 => 'zero', 1 => 'one', 2 => 'two'];
array_splice($arr, 1, 1);
sort($arr);
print_r($arr);
?>
A2
B3
C1
D0
Attempts:
2 left
💡 Hint
array_splice removes one element at index 1, then sort reindexes the array.

Practice

(1/5)
1. Which PHP function sorts an array and keeps the original keys intact?
easy
A. rsort()
B. sort()
C. ksort()
D. asort()

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]
Hint: 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
2. Which of the following is the correct syntax to sort an array in ascending order and reset keys in PHP?
easy
A. asort($array);
B. sort($array);
C. ksort($array);
D. arsort($array);

Solution

  1. Step 1: Identify function for ascending sort with key reset

    sort() sorts array values in ascending order and resets keys.
  2. Step 2: Check other options

    asort() keeps keys, ksort() sorts by keys, arsort() sorts descending.
  3. Final Answer:

    sort($array); -> Option B
  4. Quick Check:

    Ascending sort with reset keys = sort() [OK]
Hint: Use sort() to sort ascending and reset keys [OK]
Common Mistakes:
  • Using asort() when keys should reset
  • Confusing ksort() with value sort
  • Using arsort() for ascending order
3. What will be the output of the following PHP code?
$arr = [3 => 'apple', 1 => 'banana', 2 => 'cherry'];
sort($arr);
print_r($arr);
medium
A. [0 => 'apple', 1 => 'banana', 2 => 'cherry']
B. [1 => 'banana', 2 => 'cherry', 3 => 'apple']
C. [0 => 'banana', 1 => 'cherry', 2 => 'apple']
D. [0 => 'apple', 1 => 'cherry', 2 => 'banana']

Solution

  1. Step 1: Understand sort() behavior on keys

    sort() sorts values ascending and resets keys starting from 0.
  2. Step 2: Sort values and assign new keys

    Values sorted ascending: 'apple', 'banana', 'cherry'. Keys reset to 0,1,2 respectively.
  3. Final Answer:

    [0 => 'apple', 1 => 'banana', 2 => 'cherry'] -> Option A
  4. Quick Check:

    sort() resets keys and sorts ascending [OK]
Hint: sort() resets keys and sorts values ascending [OK]
Common Mistakes:
  • Expecting original keys to remain
  • Confusing sort() with asort()
  • Mixing order of sorted values
4. Identify the error in the following PHP code snippet:
$arr = ['b' => 2, 'a' => 1, 'c' => 3];
ksort($arr, SORT_DESC);
print_r($arr);
medium
A. ksort() sorts values, not keys
B. Array keys must be numeric for ksort()
C. ksort() does not accept SORT_DESC as a parameter
D. print_r() cannot print associative arrays

Solution

  1. Step 1: Check ksort() parameters

    ksort() sorts array by keys in ascending order only; it does not support descending order.
  2. Step 2: Understand SORT_DESC usage

    SORT_DESC is invalid for ksort(); to sort keys descending, use krsort() instead.
  3. Final Answer:

    ksort() does not accept SORT_DESC as a parameter -> Option C
  4. Quick Check:

    ksort() only sorts keys ascending [OK]
Hint: Use krsort() for descending key sort, not ksort() [OK]
Common Mistakes:
  • Passing SORT_DESC to ksort()
  • Thinking ksort() sorts values
  • Believing print_r() can't print associative arrays
5. 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?
hard
A. arsort()
B. rsort()
C. krsort()
D. sort()

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]
Hint: 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
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard