W3docs

usort()

Introduction

usort()

In the world of web development, PHP is a popular scripting language used for creating dynamic and interactive websites. One of the many functions that PHP offers is the usort() function, which is used for sorting arrays in a specific order using a user-defined comparison function. In this article, we will discuss the usort() function in detail and demonstrate how it can be used to sort arrays with custom sorting rules.

What is usort() function?

The usort() function is a built-in PHP function that allows you to sort arrays based on your own custom sorting rules. Unlike standard sorting functions that sort by value or key in ascending/descending order, usort() requires a callback function to define the comparison logic. It modifies the original array in place and returns true on success or false on failure. With usort(), you can define your own custom sorting rules to sort arrays in any order you desire.

Note: usort() is not a stable sort. If two elements compare as equal, their original relative order is not guaranteed to be preserved. For production code requiring stable sorting, consider using a stable sorting algorithm or pre-sorting with a stable function. Additionally, usort() reindexes numeric array keys sequentially starting from 0.

Syntax

The syntax for the usort() function is as follows:

The syntax of usort() function in PHP

usort($array, $callback);

Here, $array is the array that you want to sort, and $callback is the callback function that defines the custom sorting rules.

Parameters and Return Value

ParameterTypeDescription
$arrayarrayThe array to sort.
$callbackcallableA user-defined comparison function.

Return Value: Returns true on success or false on failure.

Example Usage

Let's take a look at an example to see how the usort() function works. Suppose we have an array of names that we want to sort in alphabetical order, but with a custom rule that all names starting with "J" should come first.

How to use usort() function in PHP?

<?php

$names = ["John", "Adam", "Jack", "Jenny", "Bob"];
usort($names, function ($a, $b) {
    if (substr($a, 0, 1) === 'J' && substr($b, 0, 1) !== 'J') {
        return -1;
    } elseif (substr($a, 0, 1) !== 'J' && substr($b, 0, 1) === 'J') {
        return 1;
    } else {
        return $a <=> $b;
    }
});

print_r($names);

In this example, we define a callback function that compares two names and returns -1, 0, or 1 based on the custom sorting rules. If the first letter of $a is "J" and the first letter of $b is not "J", then $a comes before $b. If the first letter of $a is not "J" and the first letter of $b is "J", then $b comes before $a. If both names start with "J" or both do not start with "J", then we use the modern PHP spaceship operator (<=>) to compare the names alphabetically.

After executing this code, the $names array will be sorted as follows:

Array
(
    [0] => Jack
    [1] => Jenny
    [2] => John
    [3] => Adam
    [4] => Bob
)

Conclusion

In this article, we discussed the usort() function in PHP and demonstrated how it can be used to sort arrays with custom sorting rules. By defining a callback function, you can sort arrays in any order you desire, based on your own custom rules. This function can be especially useful in situations where the built-in sorting functions in PHP are not sufficient for your needs. With the usort() function, you have complete control over how your arrays are sorted.

Practice

Practice

What is the purpose of the usort() function in PHP?