PHP uasort()Function

PHP Array ReferenceComplete PHP Array Reference Manual

Example

Sort the elements in 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 example »

Definition and Usage

uasort() sorts an array by key value using a user-defined comparison function.

Tip:Please useuksort()the function to sort the array by key name; this function sorts using a user-defined comparison function.

Syntax

uasort(array,myfunction);

Parameter 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 <, =, > 0 respectively.

Technical Details

Return value: Returns TRUE on success, or FALSE on failure.
PHP Version: 4+


PHP Array ReferenceComplete PHP Array Reference Manual Other Extensions