Skip to content
PHP Logo

Is there a specific reason for sorting the array in this particular order?

When sorting an array in PHP, the default behavior is to sort the elements in ascending order based on their values. However, if you need to sort the array in a specific order, such as sorting strings in a case-insensitive manner or sorting numbers in descending order, you can use a custom sorting function to achieve this.

php
// Sorting an array of strings in a case-insensitive manner
$array = ["Apple", "banana", "orange", "grape"];
usort($array, function($a, $b) {
    return strcasecmp($a, $b);
});
print_r($array);