Bird
Raised Fist0
PHPprogramming~10 mins

Array sort functions in PHP - Step-by-Step Execution

Choose your learning style10 modes available
Concept Flow - Array sort functions
Start with array
↓
Choose sort function
↓
Compare elements
↓
Swap if needed
↓
Repeat until sorted
↓
Return sorted array
The array is sorted by repeatedly comparing and swapping elements until the whole array is ordered.
Execution Sample
PHP
<?php
$arr = [3, 1, 4, 2];
sort($arr);
print_r($arr);
?>
Sorts the array in ascending order and prints the sorted array.
Execution Table
StepArray StateActionExplanationOutput
1[3, 1, 4, 2]Start sortingInitial array before sorting
2[1, 3, 4, 2]Swap 3 and 13 > 1, swap to order ascending
3[1, 3, 4, 2]Compare 3 and 43 < 4, no swap
4[1, 3, 2, 4]Swap 4 and 24 > 2, swap to order ascending
5[1, 2, 3, 4]Swap 3 and 23 > 2, swap to order ascending
6[1, 2, 3, 4]Array sortedNo more swaps needed[1, 2, 3, 4]
💡 Array is fully sorted in ascending order, sort() function ends.
Variable Tracker
VariableStartAfter Step 2After Step 4After Step 5Final
arr[3, 1, 4, 2][1, 3, 4, 2][1, 3, 2, 4][1, 2, 3, 4][1, 2, 3, 4]
Key Moments - 3 Insights
Why does the array change after some steps but not others?
Only when two elements are out of order does the sort function swap them, as shown in steps 2, 4, and 5 in the execution_table.
Does sort() change the original array or create a new one?
sort() changes the original array in place, which you can see in variable_tracker where 'arr' updates after swaps.
Why is the output empty until the last step?
The output is only shown after sorting completes; intermediate steps show array changes but no output until the final sorted array is printed.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table at step 2, what is the array state?
A[3, 1, 4, 2]
B[1, 3, 4, 2]
C[1, 2, 3, 4]
D[3, 4, 1, 2]
💡 Hint
Check the 'Array State' column at step 2 in the execution_table.
At which step does the array become fully sorted?
AStep 4
BStep 5
CStep 6
DStep 3
💡 Hint
Look for the step where 'Array sorted' is noted in the 'Action' column.
If the initial array was [5, 4, 3, 2], what would happen at step 2?
ASwap 5 and 4
BNo swap, array stays the same
CSwap 4 and 3
DArray is already sorted
💡 Hint
Refer to how sort swaps elements when the left is greater than the right in the execution_table steps.
Concept Snapshot
PHP array sort functions:
- Use sort() to sort arrays ascending.
- sort() changes the original array.
- Elements are compared and swapped until sorted.
- Output is the sorted array.
- Works with numbers and strings.
Full Transcript
This visual shows how PHP's sort() function works step-by-step. We start with an array of numbers. The function compares pairs of elements and swaps them if they are out of order. This process repeats until the whole array is sorted in ascending order. The variable 'arr' changes after each swap, showing the array's new order. The output is only shown after sorting completes. This helps beginners see how sorting rearranges elements inside the original 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