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

Description

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

Example


  /* w  ww  . jav  a2 s.  c om*/
<?php
    function my_sort($a,$b){
       if ($a==$b) 
          return 0;
       return ($a<$b)?-1:1;  
    }
   
    $a=array(4,2,8,6);
    usort($a,"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