php 多维数组排序


今天分享的是支持多位数组的,不管几位数组都支持的

<?php
 
$array=array(
    array(
        "one"=>array(
                
                "two"=>array(
                        "three"=>3,
                        "num"=>array(
                            'num'=>1
                        )
                    ),
                "num"=>12,
                
            ),
 
    )
    ,
    array(
        "one"=>array(
                
                "two"=>array(
                        "three"=>2,
                        "num"=>array(
                            'num'=>2
                        )
                    ),
                "num"=>11,
                
            ),
 
    )
    ,
    array(
        "one"=>array(
                
                "two"=>array(
                        "three"=>1,
                        "num"=>array(
                            'num'=>1
                        )
                    ),
                "num"=>13,
                
            ),
 
    )
);
 
 
$arr=my_array_multisort($array,'num');
print_r($arr);
 
function my_array_multisort($data,$sort_order_field,$sort_order=SORT_ASC,$sort_type=SORT_NUMERIC){ 
    foreach($data as $key=> $val){
        if( $val && count($val) && is_array($val)){
            $sort_array= sort_order_field($val,$sort_order_field);            
        }
        
        $key_arrays[]=$sort_array[$sort_order_field];
    }
    array_multisort($key_arrays,$sort_order,$sort_type,$data);
    return $data;
}
function sort_order_field($data,$sort_order_field){
    if($data && count($data) && is_array($data)){
        foreach ($data as $key => $value) {
            if($sort_order_field==$key && !is_array($value)){ 
 
                return $data;                
            }
            if($value && count($value) && is_array($value)){
                $temp_data= sort_order_field($value,$sort_order_field);                
                if(!empty($temp_data[$sort_order_field] && !is_array($temp_data[$sort_order_field]))){
                    return $temp_data;
                }
 
            }
        }
    }
 
    return $data;
}
?>


尊云CMS
请先登陆后发表评论
  • 最新评论
  • 总共0条评论