PHP uksort()Functions

PHP Array ReferenceComplete PHP Array Reference

Examples

Sort the elements of 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 an array by key names using a user-defined comparison function.

Tip:Please use theuasort()function to sort the array by key values, which uses a user-defined comparison function for sorting.

Syntax

uksort(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 Other Extensions