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.
//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 "<h3>The values of the date array:</h3>";
foreach($date_array as $value)
{
echo $value."<br />";
}
//Sort the array using usort()
usort($date_array, "sort_date");
//Print the array after sort
echo "<h3>The output after sorting date array:</h3>";
foreach($date_array as $value)
{
echo $value."<br />";
}
?>
Output:
The following output will appear after running the script from the server.
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.
//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 "<b>The values of the sorted array:</b><br />";
foreach($persons as $person)
echo $person. "<br />";
?>
Output:
The following output will appear after running the script from the server.
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.
//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 "<b>The array values after usort():</b><br /><pre>";
print_r($animals);
echo "</pre>";
?>
Output:
The following output will appear after running the script from the server.
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.