Sort the elements of the $arr array by keys using a user-defined comparison function in PHP

Description

The following code shows how to sort the elements of the $arr array by keys using a user-defined comparison function.

Example


  //from  w  w w . j a v  a2  s . co  m
<?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");
?>




















Home »
  PHP Tutorial »
    Data Types »




Array
Array Associative
Array Util
ArrayObject
Data Types
Date
Date Format
DateTime
Number
String
String Escape
String Filter
String HTML
String Type
Timezone