PHP uasort() function

Example

Sort the elements of the array $arr by key value using a user-defined comparison function:

<?php
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");
?>

Run Instance

Definition and Usage

The uasort() function sorts the array using a user-defined comparison function and maintains index association (does not assign new keys to elements).

Returns TRUE if successful, otherwise FALSE.

This function is mainly used to sort composite arrays where the order of units is very important.

Hint:Use uksort() The function sorts the array by key name using a user-defined comparison function.

Syntax

uasort(array,myfunction);
Parameter Description
array Required. Specifies the array to be sorted.
myfunction Optional. Define a string that specifies the comparison function to be called. If the first parameter is less than or equal to or greater than the second parameter, the comparison function must return an integer less than or equal to or greater than 0.

Technical Details

Return value: Returns TRUE if successful, otherwise FALSE.
PHP Version: 4+