PHP | usort() Function
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.
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:
Array (  => 1  => 2  => 3  => 5  => 9 )