PHP uksort() Function

Example

Sort the elements of the array $arr by key name 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");
uksort($arr,"my_sort");
?>

Run Example

Definition and Usage

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

Tip:Please use uasort() The function sorts the array by key value using a user-defined comparison function, which uses a user-defined comparison function for sorting.

Syntax

uksort(array,myfunction);
Parameter Description
array Required. Specifies the array to be sorted.
myfunction Optional. Defines the string that defines the callable comparison function. If the first parameter is less than, equal to, or greater than the second parameter, then the comparison function must return an integer less than, equal to, or greater than 0.

Description

The uksort() function uses a user-defined comparison function to sort the array by key name and maintains the index relationship.

Returns TRUE on success, otherwise FALSE.

If the array to be sorted needs to be sorted by an unusual standard, then this function should be used.

Custom functions should accept two parameters, which will be filled with a pair of key names from the array. The comparison function must return an integer less than, equal to, or greater than zero respectively when the first parameter is less than, equal to, or greater than the second parameter.

Technical Details

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