← Back to PHP Course | Chapter 7: Arrays | Lesson 11 of 13

PHP Array Sorting

Sorting an array is like lining up classmates by height or by name. PHP can put a list in order for you, smallest to biggest, biggest to smallest, or A to Z.
Syntax
php
sort($array);      // ascending values, reindexed
rsort($array);     // descending
asort($array);     // by value, keep keys
ksort($array);     // by key
arsort($array);
krsort($array);

Sorting Indexed Arrays

sort() reorders an indexed array's values in ascending order and reindexes the keys from 0, which is what you want for a simple list but will destroy any meaningful string keys in an associative array.

Example: Sorting Indexed Arrays

php
<?php
// Declare `$numbers` as an array: `[3, 1, 4, 1, 5]`
$numbers = [3, 1, 4, 1, 5];
// Call `sort($numbers)`
sort($numbers);
// Print a human-readable dump of `$numbers`
print_r($numbers);
?>

Sorting Associative Arrays by Value

asort() and ksort() preserve key-to-value associations while sorting: asort() sorts by value, ksort() sorts by key, both keeping the original pairing intact — essential when the keys carry meaning, like usernames mapped to scores.

Example: Sorting Associative Arrays by Value

php
<?php
// Declare `$scores` as an array: `["alice" => 90, "bob" => 75]`
$scores = ["alice" => 90, "bob" => 75];
// Call `asort($scores)`
asort($scores);
// Print a human-readable dump of `$scores`
print_r($scores);
// Call `ksort($scores)`
ksort($scores);
// Print a human-readable dump of `$scores`
print_r($scores);
?>

Sorting Associative Arrays by Key

rsort(), arsort(), and krsort() are the descending-order counterparts of sort(), asort(), and ksort() respectively, useful for tasks like showing the highest scores first on a leaderboard.

Example: Sorting Associative Arrays by Key

php
<?php
// Declare `$scores` as an array: `["alice" => 90, "bob" => 75, "carol" => 85]`
$scores = ["alice" => 90, "bob" => 75, "carol" => 85];
// Call `arsort($scores)`
arsort($scores);
// Print a human-readable dump of `$scores`
print_r($scores);
?>

Custom Sorting with usort

usort() lets you supply your own comparison function, giving full control over sort order for cases the built-in functions can't handle, such as sorting an array of associative arrays by one specific field.

Example: Custom Sorting with usort

php
<?php
// Declare `$people` as an array: `[["name" => "Bob", "age" => 25], ["name" => "Alice", "age" => 30]]`
$people = [["name" => "Bob", "age" => 25], ["name" => "Alice", "age" => 30]];
usort($people, function ($a, $b) {
    // Return `$a["age"] <=> $b["age"]` from this function
    return $a["age"] <=> $b["age"];
});
// Print a human-readable dump of `$people`
print_r($people);
?>

Natural Order Sorting

Sorting functions in PHP modify the array in place and return true/false rather than returning a new sorted array, which is a common gotcha if you expect $sorted = sort($array); to work — it doesn't, sort($array) is what you need.

Example: Natural Order Sorting

php
<?php
$numbers = [3, 1, 2];
sort($numbers); // modifies in place, returns true/false
print_r($numbers);
?>
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
Common Mistakes
  1. Expecting sort on an associative array to keep its keys, when sort discards them; use asort or ksort.
  2. Using sort($arr) as an expression, such as $b = sort($a);, when it sorts in place and returns true.
  3. Writing a usort callback that returns a boolean instead of a negative, zero or positive number.

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.