Apr 232012
 

I got a sloppy version of this somewhere off the Internet years ago. It’s been cleaned up quite a bit and should be easier to understand.

  /**
   * Multi-dimensional array sort. Returns $array sorted according to the 
   *   parameters given
   * @param mixed $array - The array to be sorted
   * @param mixed $index - The array key by which to sort the array
   * @param mixed $order - "asc" for ascending, otherwise, descending
   * @param boolean $natsort - true for natural order sorting, false otherwise
   * @param boolean $case_sensitive - true for case sensitivity, false otherwise
   * @return mixed - Returns $array sorted according to the parameters given
   */
  public static function mdaSort ($array,
                                  $index,
                                  $order='asc',
                                  $natsort=FALSE,
                                  $case_sensitive=FALSE) {

    $result = $array;
    $temp   = array();

    if(is_array($array) && count($array)>0) {

        foreach(array_keys($array) as $key) {
          $temp[$key]=$array[$key][$index];
        }

        if(!$natsort) {
          ($order=='asc') ? asort($temp) : arsort($temp);

        } else {
          ($case_sensitive) ? natsort($temp) : natcasesort($temp);
          if($order!='asc') {
            $temp=array_reverse($temp,TRUE);
          }
        }

        foreach(array_keys($temp) as $key) {
          (is_numeric($key)) ? $sorted[]=$array[$key] : $sorted[$key]=$array[$key];
        }

        $result = $sorted;
      }

    return $result;
  }
 Posted by at 8:00 am