← All built-ins / usort()

usort()

usort(...$args): bool

Parameters

NameTypeDescription
$aarrayarray to sort (in place)
$cmpclosurecomparator 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

See also

sort()uasort()uksort()