PHP usort() Function Last Updated : 20 Jun, 2023 Comments Improve Suggest changes 1 Likes Like Report PHP comes with a number of built-in functions that are used to sort arrays in an easier way. Here, we are going to discuss a new function usort(). The usort() function in PHP sorts a given array by using a user-defined comparison function. This function is useful in case if we want to sort the array in a new manner. This function assigns new integral keys starting from zero to the elements present in the array and the old keys are lost. Syntax: boolean usort( $array, "function_name"); Parameters: This function accepts two parameters as shown in the above syntax and are described below: $array: This parameter specifies the array which u want to sort. function_name : This parameter specifies the name of a user-defined function which compares the values and sort the array specified by the parameter $array. This function returns an integer value based on the following conditions. If two arguments are equal, then it returns 0, If first argument is greater than second, it returns 1 and if first argument is smaller than second, it returns -1. Return Value: This function returns the boolean type of value. It returns TRUE in case of success and FALSE in case of failure. Below program illustrate the usort() function in PHP: PHP <?php // PHP program to illustrate usort() function // This is the user-defined function used to compare // values to sort the input array function comparatorFunc( $x, $y) { // If $x is equal to $y it returns 0 if ($x== $y) return 0; // if x is less than y then it returns -1 // else it returns 1 if ($x < $y) return -1; else return 1; } // Input array $arr= array(2, 9, 1, 3, 5); usort($arr, "comparatorFunc"); print_r($arr); ?> Output: Array ( [0] => 1 [1] => 2 [2] => 3 [3] => 5 [4] => 9 ) Reference: https://www.php.net/manual/en/function.usort.php Comment S Shivani2609 Follow 1 Improve S Shivani2609 Follow 1 Improve Article Tags : Misc Web Technologies PHP PHP-array PHP-function +1 More Explore PHP Tutorial 8 min read BasicsPHP Syntax 4 min read PHP Variables 5 min read PHP | Functions 8 min read PHP Loops 4 min read ArrayPHP Arrays 5 min read PHP Associative Arrays 4 min read Multidimensional arrays in PHP 5 min read Sorting Arrays in PHP 4 min read OOPs & InterfacesPHP Classes 2 min read PHP | Constructors and Destructors 5 min read PHP Access Modifiers 4 min read Multiple Inheritance in PHP 4 min read MySQL DatabasePHP | MySQL Database Introduction 4 min read PHP Database connection 2 min read PHP | MySQL ( Creating Database ) 3 min read PHP | MySQL ( Creating Table ) 3 min read PHP AdvancePHP Superglobals 6 min read PHP | Regular Expressions 12 min read PHP Form Handling 4 min read PHP File Handling 4 min read PHP | Uploading File 3 min read PHP Cookies 9 min read PHP | Sessions 7 min read Like