PHP VERSION: 8.2.18

spaceship_operator_usort_example.php

<?php
$array 
= [  'oranges',
            
'apples',
            
'bananas',
            
'grapes'];
echo 
'Before: ' PHP_EOL;
print_r($array);
usort($array, function ($left$right) { return $left <=> $right; });
echo 
'After: ' PHP_EOL;
print_r($array);

Output


Before: 
Array
(
    [0] => oranges
    [1] => apples
    [2] => bananas
    [3] => grapes
)
After: 
Array
(
    [0] => apples
    [1] => bananas
    [2] => grapes
    [3] => oranges
)
SOURCE CODE