What are some common use cases for sorting arrays in PHP?

When working with arrays in PHP, it is often necessary to sort the elements in a specific order. This can be useful for organizing data, displaying information in a certain way, or preparing data for further processing. Common use cases for sorting arrays in PHP include sorting numbers in ascending or descending order, sorting strings alphabetically, and sorting multidimensional arrays based on a specific key.

// Sorting numbers in ascending order
$numbers = [5, 2, 8, 1, 9];
sort($numbers);
print_r($numbers);

// Sorting strings alphabetically
$fruits = ["apple", "orange", "banana", "grape"];
sort($fruits);
print_r($fruits);

// Sorting multidimensional arrays based on a specific key
$students = [
    ["name" => "Alice", "age" => 25],
    ["name" => "Bob", "age" => 20],
    ["name" => "Charlie", "age" => 22]
];
usort($students, function($a, $b) {
    return $a['age'] - $b['age'];
});
print_r($students);