PHP Tutorial - PHP uksort() Function






Definition

The uksort() function sorts an array by keys using a user-defined comparison function.

Syntax

PHP uksort() Function has the following syntax.

uksort(array,myfunction);

Parameter

ParameterIs RequiredDescription
arrayRequired.Array to sort
myfunctionOptional.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 keys using a user-defined comparison function:


<?php/*from w  ww .  j a v  a2s  .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");
uksort($arr,"my_sort");
?>