Many built-in functions exist in PHP to sort the array variables. usort() function is one of them. This function sorts the array by using a user-defined callback function. When the array contains a particular type of data that can’t be sort in a standard way by using other sort functions, then usort() is better to use. For example, if the array contains data values, then the variety can’t be appropriately sorted using other sort functions of PHP. This type of collection can be sort by defining the proper user-defined function called in the second argument of the usort() function how usort() function can sort the specific array values shown in this tutorial.

Syntax:

The syntax of the usort() function is given below.

This function has two arguments. Both arguments are mandatory. The first argument takes the array that will be sorted. The callback() function compares the array’s values to sort the array and returns a numeric value. If the arguments of the callback() function are equal, then the function will return 0. If the first argument of the callback() function is greater than the second argument, it will return 1. If the first argument of the callback() function is smaller than the second argument, it returns -1. usort() function returns true for successful sort and returns false for unsuccessful sort. Some uses of this function have shown in the next part of this tutorial.

Example-1: Sort an array of date values

The following example shows how an array of date values can be sorted properly using the usort() function. Create a PHP file with the following script. $date_array is defined with five-date values. sort_date() function is declared as callback function to sort the $date_array. The array values are printed before calling the usort() function. When the usort() function is called with the array and the callback function, it will convert two date values into timestamp values using the strtotime() function. If the two timestamp values are equal, then the function will return 0. If the first timestamp value is greater than the second timestamp value, then the function will return 1. If the first timestamp value is lower than the second timestamp value, the function will return -1. usort() function will call the callback function multiple times until the $date_array is sorted properly. Next, the sorted array will be printed using the for each loop.

<?php

//Define the callback function

function sort_date($a, $b) {


    return strtotime($a) strtotime($b);

}

//Declare the array of date values

$date_array = array(’25-03-2020′, ’14-03-2020′, ’28-02-2015′, ’09-12-2017′, ’20-09-2015′);

//Print the array values before sort

echo

The values of the date array:

;

foreach($date_array as $value)

{


    echo $value.
;

}

//Sort the array using usort()

usort($date_array, “sort_date”);

//Print the array after sort

echo

The output after sorting date array:

;

foreach($date_array as $value)

{


    echo $value.
;

}

?>

Output:

The following output will appear after running the script from the server.

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/01/echo/us1.png" data-lazy- height="496" src="data:image/svg xml,” width=”1094″>

Example-2: Sort an associative array

The following example shows how an associative array can be sorted using the usort() function. Create a PHP file with the following script. sort_names() function is declared a callback function to sort the array based on the array values. The ternary operator is used in the sort_names() function to compare the associative array’s values. If the two values are equal, then the callback function will return 0. If the first argument value is greater than the second argument value, then the callback function will return 1. If the first argument value is smaller than the second argument value, then the callback function will return -1. usort() will sort the array by using the callback function, and the sorted array will be printed using the for each loop.

<?php

//Define the function to sort the associative array

function sort_names($a, $b) {

return $a == $b ? 0 : $a > $b ? 1 : 1;

}

//Define the associative array

$persons = array(“1001”=>“Meera Ali”, “1002”=>“Kabir Hossain”, “1003”=>“Nurjahan Akter”);

//Sort the array

usort($persons, “sort_names”);

//Print the sorted array

echo The values of the sorted array:
;

foreach($persons as $person)

echo $person.
;

?>

Output:

The following output will appear after running the script from the server.

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/01/echo/us2.png" data-lazy- height="208" src="data:image/svg xml,” width=”1087″>

Example-3: Sort a two-dimensional array

The following example shows how a two-dimensional array can be sorted using the usort() function. Create a PHP file with the following script. sort_array() function is declared a callback function to sort the array based on the array values. strcmp() function is used in the callback function to compare the values of the array. If the two values are equal, then the strcmp() function will return 0. If the first argument value is greater than the second argument value, then the strcmp() function will return 1. If the first argument value is smaller than the second argument value, then the strcmp() function will return -1. usort() will sort the array by using the callback function, and the structure of the sorted array will be printed using the print_r() function.

<?php

//Define function to sort two-dimensional array

function sort_array($a, $b)

{


    return strcmp($a[‘a’], $b[‘a’]);

}

//Declare the two-dimentional array

$animals = array(array (“a” => “Lion”), array (“a” => “Deer”), array (“a” => “Rabbit”),array (“a” => “Monkey”));

//Sort the array

usort($animals, “sort_array”);

//Print the sorted array

echo The array values after usort():

";

print_r($animals);

echo "

;

?>

Output:

The following output will appear after running the script from the server.

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/01/echo/us3.png" data-lazy- height="532" src="data:image/svg xml,” width=”1090″>

Conclusion:

usort() is a useful function for sorting particular types of data that cannot correctly sort PHP’s normal sort functions. The usort() function’s callback function is defined based on the kinds of values that are required to sort. This tutorial will help the readers know how to use the usort() function and apply it in their script based on the requirement.

About the author

<img alt="Fahmida Yesmin" data-lazy-src="https://kirelos.com/wp-content/uploads/2021/01/echo/channel-logo-150×150.jpg6003c2c824d5e.jpg" height="112" src="data:image/svg xml,” width=”112″>

Fahmida Yesmin

I am a trainer of web programming courses. I like to write article or tutorial on various IT topics. I have a YouTube channel where many types of tutorials based on Ubuntu, Windows, Word, Excel, WordPress, Magento, Laravel etc. are published: Tutorials4u Help.