PHP 8 reference · Array Sorting Functions
PHP uasort() Function
Sort an array by value with your own comparison function, keeping keys attached to their values.
PHP 8 Since 4.0
Syntax
uasort(array &$array, callable $callback): true
Parameters
| Parameter | Description |
|---|---|
array |
The array to sort (changed in place). |
callback |
fn($a, $b) returns a negative number, 0 or a positive number; $a <=> $b does this for you. |
Return value
Always true.
Examples
<?php
$users = [
"u7" => ["name" => "Alan", "age" => 41],
"u3" => ["name" => "Ada", "age" => 36],
];
uasort($users, fn($a, $b) => $a["age"] <=> $b["age"]);
print_r(array_keys($users));
Output
Array
(
[0] => u3
[1] => u7
)<?php
$files = ["a.txt" => "2026-03-01", "b.txt" => "2025-12-24", "c.txt" => "2026-01-15"];
uasort($files, fn($x, $y) => strcmp($y, $x)); // newest first
print_r($files);
Output
Array
(
[a.txt] => 2026-03-01
[c.txt] => 2026-01-15
[b.txt] => 2025-12-24
)Version history
Related
usort()· Sort an array's values with your own comparison function, in place. Keys are discarded. Use <=> for easy comparisons.uksort()· Sort an array by key with your own comparison function, keeping values with their keys.asort()· Sort an array by value in ascending order, keeping each key attached to its value. Ideal for name => score maps.- PHP Arrays · tutorial
- PHP Array Functions · tutorial