二维数组按某字段排序算法

/**
 * 二维数组按某字段排序算法
 * @param array $arrUsers 要排序的数组
 * @param string $field 排序字段
 * @param string $direction 排序顺序标志 SORT_DESC 降序;SORT_ASC 升序
 * @return array 返回排序后的数组
 * @author mosishu
 */
function array_sort($arrUsers,$field,$direction){
    if ($direction=='ASC' || $direction=='asc' || $direction=='SORT_ASC'){
        $order = 'SORT_ASC';
    }elseif ($direction=='DESC' || $direction=='desc' || $direction=='SORT_DESC'){
        $order = 'SORT_DESC';
    }else{
        return $arrUsers;
    }
    $sort = array(
        'direction' => $order, //排序顺序标志 SORT_DESC 降序;SORT_ASC 升序
        'field'     => $field, //排序字段
    );
    $arrSort = array();
    foreach($arrUsers AS $uniqid => $row){
        foreach($row AS $key=>$value){
            $arrSort[$key][$uniqid] = $value;
        }
    }
    if($sort['direction']){
        array_multisort($arrSort[$sort['field']], constant($sort['direction']), $arrUsers);
    }
    return $arrUsers;
}

阅读更多
换一批

没有更多推荐了,返回首页