PHP Usort () Function

PHP

Syntax:
boolean usort ($array, "function_name");
Parameters:This function takes two parameters as shown in the above syntax and described below:
  • $array : This parameter specifies the array you want to sort.
  • function_name : This parameter specifies the name of a custom function that compares values ​​and sorts the array specified by $array. This function returns an integer value based on the following conditions. If the two arguments are equal, it returns 0, if the first argument is greater than the second, it returns 1, and if the first argument is less than the second, it returns -1.
  • Return value: this function returns a boolean value type. Returns TRUE on success and FALSE on failure.The following program illustrates the usort() function in PHP: // PHP program to illustrate the usort() function // This is a custom function used for comparison // values ​​for sorting the input array function comparatorFunc ( $x , $y ) { // If $x is $y, 0 is returned if ( $x == $y ) return 0; // if x is less than y, then -1 is returned // otherwise 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)
    Link:
    http://php.net/manual/en/function.usort.php