php数组顺序排序函数

/**
 * 数组顺序排序函数
 * by hkshadow
 * 2011-04-07
 * Enter description here ...
 * @param 数组 $a
 * @param 字段 $sort
 * @param 排序方式,默认为降序 $d
 */
function Array_Sort($a,$sort,$d='') {
    $num=count($a); //统计数组
    if(!$d){ //如果不选择排序方式,则默认降序排序
        for($i=0;$i<$num;$i++){
            for($j=0;$j<$num-1;$j++){
                if($a[$j][$sort] > $a[$j+1][$sort]){   //$a数组   $j键值   $sort字段
                    foreach ($a[$j] as $key=>$temp){   
                        $t=$a[$j+1][$key];
                        $a[$j+1][$key]=$a[$j][$key];
                        $a[$j][$key]=$t;
                    }
                }
            }
        }
    }
    else{
        for($i=0;$i<$num;$i++){
            for($j=0;$j<$num-1;$j++){
                if($a[$j][$sort] < $a[$j+1][$sort]){  //$a数组   $j键值   $sort字段
                    foreach ($a[$j] as $key=>$temp){
                        $t=$a[$j+1][$key];
                        $a[$j+1][$key]=$a[$j][$key];
                        $a[$j][$key]=$t;
                    }
                }
            }
        }
    }
    return $a;
}

One Response to “php数组顺序排序函数”

Leave a Reply

(will not be published)