sort() / rsort() / asort() / arsort() Since: PHP 4(2000)
Functions for sorting array elements by value or key in ascending or descending order. All of these functions modify the original array directly.
Syntax
// Sorts by value in ascending order. Keys are reassigned. sort(array, sort_flags); // Sorts by value in descending order. Keys are reassigned. rsort(array, sort_flags); // Sorts by value in ascending order. Key-value associations are preserved. asort(array, sort_flags); // Sorts by value in descending order. Key-value associations are preserved. arsort(array, sort_flags); // Sorts by key in ascending order. ksort(array, sort_flags); // Sorts by key in descending order. krsort(array, sort_flags);
Function List
| Function | Description |
|---|---|
| sort($array, $flags) | Sorts an array by value in ascending order. Numeric keys are reassigned starting from 0. |
| rsort($array, $flags) | Sorts an array by value in descending order. Numeric keys are reassigned starting from 0. |
| asort($array, $flags) | Sorts an array by value in ascending order. Key-value associations are preserved. |
| arsort($array, $flags) | Sorts an array by value in descending order. Key-value associations are preserved. |
| ksort($array, $flags) | Sorts an array by key in ascending order. |
| krsort($array, $flags) | Sorts an array by key in descending order. |
Sample Code
<?php // Sort by value in ascending order. $numbers = [3, 1, 4, 1, 5, 9, 2, 6]; sort($numbers); print_r($numbers); // Results in ascending order: 1, 1, 2, 3, 4, 5, 6, 9. // Sort by value in descending order. $scores = [85, 92, 78, 95, 88]; rsort($scores); print_r($scores); // Results in descending order: 95, 92, 88, 85, 78. // Sort an associative array by value in ascending order. Keys are preserved. $prices = ['apple' => 150, 'orange' => 80, 'grape' => 300, 'peach' => 200]; asort($prices); print_r($prices); // Sorted by ascending value, with key associations maintained. // Sort an associative array by value in descending order. arsort($prices); print_r($prices); // Sorted by descending value, with key associations maintained. // Sort by key in ascending order. $settings = ['z_index' => 100, 'color' => 'red', 'margin' => 10, 'border' => '1px']; ksort($settings); print_r($settings); // Sorted alphabetically by key. // Sort strings containing numbers using natural order sorting. $files = ['file10.txt', 'file2.txt', 'file1.txt', 'file20.txt']; sort($files, SORT_NATURAL); print_r($files); // Sorted in natural numeric order.
Notes
All PHP sort functions are destructive — they modify the original array directly and return true on success. sort() and rsort() reassign numeric keys starting from 0, so using them on associative arrays will cause keys to be lost. Use asort(), arsort(), ksort(), or krsort() when sorting associative arrays.
The second argument accepts sort flags to control how values are compared. Common flags include SORT_REGULAR, SORT_NUMERIC, SORT_STRING, and SORT_NATURAL. SORT_NATURAL sorts strings containing numbers in the order a human would expect.
To sort with custom comparison logic, use usort() / uasort() / uksort().
If you find any errors or copyright issues, please contact us.