Return to Snippet

Revision: 57087
at May 14, 2012 18:57 by pruntrut

Initial Code
function sort2d ($array, $index, $order='asc', $natsort=FALSE, $case_sensitive=FALSE) 
        if(is_array($array) && count($array)>0) 
           foreach(array_keys($array) as $key) 
                   ($order=='asc')? asort($temp) : arsort($temp);
                 ($case_sensitive)? natsort($temp) : natcasesort($temp);
           foreach(array_keys($temp) as $key) 
               (is_numeric($key))? $sorted[]=$array[$key] : $sorted[$key]=$array[$key];
           return $sorted;
      return $array;

Initial URL

Initial Description
The following shoud do it. pass the array, index is what element you want to sort on (in your case use 3). Then you can sort asc or desc, and weather you want to use natural sorting (the way a human would sort, 2 before 10) and lastly case sensitive or not if using natural sort. Only the first 2 args are needed, the rest will default.

Initial Title
Sorting an multidimensional array

Initial Tags
php, array

Initial Language