PHP usort()Functions
Complete PHP Array Reference Manual
Examples
Sort the elements in array $a using a user-defined comparison function: 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");
?>
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");
?>
Run Example »
Definition and Usage
usort() sorts an array using a user-defined comparison function.
Syntax
usort(array,myfunction);
| Parameters | Description |
|---|---|
| array | Required. Specifies the array to sort. |
| myfunction | Optional. A string that defines a callable comparison function. If the first argument is <, =, > the second argument, the comparison function must return an integer that is <, =, > 0 respectively. |
Technical Details
| Return value: | Returns TRUE on success, or FALSE on failure. |
|---|---|
| PHP version: | 4+ |
Complete PHP Array Reference Manual Other Extensions