PHP usort() Function
In this chapter you will learn:
- Definition for PHP usort() Function
- Syntax for PHP usort() Function
- Parameter for PHP usort() Function
- Example - PHP usort() Function
Definition
The usort() function sorts an array using a user-defined comparison function.
Syntax
PHP usort() Function has the following syntax.
usort(array,myfunction);
Parameter
Parameter | Is Required | Description |
---|---|---|
array | Required. | Specifies the array to sort |
myfunction | Optional. | Name of comparison function. |
myfunction must return an integer <, =, or > than 0 if the first argument is <, =, or > than the second argument.
Example
Sorts an array using a user-defined comparison function.
<?php// j ava2s .c om
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");
?>
Next chapter...
What you will learn in the next chapter:
Home » PHP Tutorial » PHP Array Functions