PHP uasort() Function

Definition

The uasort() function sorts an array by values using a user-defined comparison function.

Syntax

PHP uasort() Function has the following syntax.

uasort(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 values using a user-defined comparison function:


<?php/* w w  w.  j  a v  a 2 s  .co 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");
uasort($arr,"my_sort");
?>




















Home »
  PHP Tutorial »
    Function reference »




PHP Array Functions
PHP Calendar Functions
PHP Class Functions
PHP Data Type Functions
PHP Date Functions
PHP File Functions
PHP Image Functions
PHP Math Functions
PHP MySQLi Functions
PHP SimpleXML Functions
PHP String Functions
PHP XML Functions
PHP Zip Functions