PHP Array Sorting
In this page:
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
// 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);
?>
Login to try C/C++/Java/PHP code in the editor
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
// 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);
?>
Login to try C/C++/Java/PHP code in the editor
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
// 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);
?>
Login to try C/C++/Java/PHP code in the editor
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
// 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);
?>
Login to try C/C++/Java/PHP code in the editor
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
$numbers = [3, 1, 2];
sort($numbers); // modifies in place, returns true/false
print_r($numbers);
?>
Login to try C/C++/Java/PHP code in the editor
- Expecting
sorton an associative array to keep its keys, whensortdiscards them; useasortorksort. - Using
sort($arr)as an expression, such as$b = sort($a);, when it sorts in place and returns true. - Writing a
usortcallback that returns a boolean instead of a negative, zero or positive number.
Chapter Quiz — Complete all 13 topics to unlock
0/13 topics done
Complete these topics first: