PHP - Function usort()
Syntax
usort ( $array, $cmp_function )
Definition and Usage
The usort() function sorts an array by a user defined comparison function. This function assigns new keys for the elements in the array. Existing keys will be removed.
Parameters
| Sr.No | Parameter & Description |
|---|---|
| 1 |
array(Required) It specifies an array. |
| 2 |
cmp_function(Required) Useful defined function to compare values and to sort them.
|
Return Value
Returns TRUE on success or FALSE on failure.
Example
Try out following example −
<?php
function cmp_function($a, $b) {
if ($a == $b) return 0;
return ($a > $b) ? -1 : 1;
}
$fruits = array("d"=>"lemon", "a"=>"orange", "b"=>"banana" );
usort($fruits, "cmp_function");
print_r($fruits);
?>
This will produce the following result −
Array ( [0] => orange [1] => lemon [2] => banana )
php_function_reference.htm
Advertisements