The spaceship operator allows for straightforward comparison of
compound values across multiple axes. The following example will sort
$people
by last name, then by first name if the
last name matches.
<?php
$people[0] = ['first' => 'Adam', 'last' => 'West'];
$people[1] = ['first' => 'Alec', 'last' => 'Baldwin'];
$people[2] = ['first' => 'Adam', 'last' => 'Baldwin'];
function sorter(array $a, array $b) {
return [$a['last'], $a['first']] <=> [$b['last'], $b['first']];
}
usort($people, 'sorter');
foreach ($people as $person) {
print $person['last'] . ', ' . $person['first'] . PHP_EOL;
}
?>
The above example will output:
Baldwin, Adam
Baldwin, Alec
West, Adam