How to use php usort() function
php usort() function definition and usage
usort() usage User-defined comparison function sorts arrays.
Syntax
usort(array,myfunction);
Parameters
array required. Specifies the array to be sorted.
myfunction Optional. A string that defines a callable comparison function. If the first argument is <, =, > and the second argument is, the corresponding comparison function must return an integer of <, =, > 0.
Example
Sort the elements of the $a array using a user-defined comparison function:
<?php function my_sort($a,$b) { if ($a==$b) return 0; return ($a<$b)?-1:1; } $a=array(4,2,8,6); usort($a,"my_sort"); ?>
Output:
2 4 6 8
The above is the detailed content of How to use php usort function. For more information, please follow other related articles on the PHP Chinese website!