← All built-ins / usort()
usort()
usort(...$args): bool
Parameters
| Name | Type | Description |
|---|---|---|
| $a | array | array to sort (in place) |
| $cmp | closure | comparator fn($x, $y) returning -1/0/1 (e.g. via <=>) |
Return value
true; the array is sorted in place.
Example
$a = [3, 1, 2];
usort($a, fn($x, $y) => $x <=> $y);
echo implode(",", $a), "
";
$a2 = [["n" => "b"], ["n" => "a"]];
usort($a2, fn($x, $y) => $x["n"] <=> $y["n"]);
echo $a2[0]["n"], "
";1,2,3 a