Skip to content
Justice Tech School

Sorting

sort and rsort sort values, asort and arsort keep keys, ksort sorts by key and usort uses your function.

Example
$a = [3, 1, 2];
sort($a); echo implode(",", $a), "\n";
$p = ["b" => 2, "a" => 3];
ksort($p); print_r($p);
usort($a, fn($x, $y) => $y <=> $x);
echo implode(",", $a);

Result

1,2,3
Array
(
    [a] => 3
    [b] => 2
)
3,2,1

Exercise

Sort by key.

($p)

Quiz Test

Which keeps keys while sorting values?

These free lessons are for learning. Join a full course for projects, mentor feedback and a verified certificate.