PHP uasort() Function
In this chapter you will learn:
- Definition for PHP uasort() Function
- Syntax for PHP uasort() Function
- Parameter for PHP uasort() Function
- Example - PHP uasort() Function
Definition
The uasort() function sorts an array by values using a user-defined comparison function.
Syntax
PHP uasort() Function has the following syntax.
uasort(array,myfunction);
Parameter
Parameter | Is Required | Description |
---|---|---|
array | Required. | Array to sort |
myfunction | Optional. | Name of the comparison function. |
myfunction must return an integer <, =, or > than 0 if the first argument is <, =, or > than the second argument.
Example
Sort the elements of the $arr array by values using a user-defined comparison function:
<?php//j ava 2 s . c o m
function my_sort($a,$b){
if ($a==$b){
return 0;
}
return ($a<$b)?-1:1;
}
$arr=array("a"=>4,"b"=>2,"c"=>8,"d"=>"6");
uasort($arr,"my_sort");
?>
Next chapter...
What you will learn in the next chapter:
- Definition for PHP uksort() Function
- Syntax for PHP uksort() Function
- Parameter for PHP uksort() Function
- Example for PHP uksort() Function
Home » PHP Tutorial » PHP Array Functions