Sorts array in place by values using a
user-supplied comparison function to determine the order.
Note:
If two members compare as equal, they retain their original order.
Prior to PHP 8.0.0, their relative order in the sorted array was undefined.
Note:
This function
assigns new keys to the elements in array.
It will remove any existing keys that may have been assigned, rather
than just reordering the keys.
Parameters
array
The input array.
callback
The comparison function must return an integer less than, equal to, or greater than zero if the first argument is considered to be respectively less than, equal to, or greater than the second.
Returning non-integer values from the comparison
function, such as float, will result in an internal cast to
int of the callback's return value. So values such as
0.99 and 0.1 will both be cast to an
integer value of 0, which will compare such values as equal.
Example #5
usort() example using the spaceship operator
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.