php多维数组排序

先看一个简答的事例
$data=array(
    array("name"=>"zhangsan","age"=>"20",'sex'=>"男"),
    array("name"=>"lisi","age"=>"19",'sex'=>"男"),
    array("name"=>"wangwu","age"=>"18",'sex'=>"男"),
    array("name"=>"zhaoliu","age"=>"22",'sex'=>"女"),
    array("name"=>"qianqi","age"=>"21",'sex'=>"女"),
);
$sort_key=array();
foreach ($data as $key=>$value)
    $sort_key[]=$value['age'];
array_multisort($sort_key,SORT_ASC,SORT_NUMERIC,$data);
print_r($data);
定义成方法
function array_sort_more($data,$sort_order_field,$sort_order=SORT_ASC,$sort_type=SORT_NUMERIC){
    foreach($data as $val){
        $key_arrays[]=$val[$sort_order_field];
    }
    array_multisort($key_arrays,SORT_ASC,SORT_NUMERIC,$data);
    rturn $data;
}

阅读更多

【解题】PHP多维数组排序问题

07-30

最近在碰PHP,刚好遇到一个问题,涉及到多维数组排序问题。该系列数组都有个关键key PID,要求按照PID将数组重新排列一遍。仅仅排序第一层array即可。使用了array_multisort(),但出现报错。如下:rn[code=php]rnArrayrn(rn [0] => Arrayrn (rn [pid] => 0rn [title] => 首页rn [path] => indexrn [current] => 0rn [link] => /index.phprn )rnrn [1] => Arrayrn (rn [tid] => 3rn [pid] => 8rn [mid] => 8rn [topid] => 0rn [upid] => 0rn [exmid] => 0rn [linkid] => 2rn [gotoid] => 17rn [lng] => cnrn [larray] => Arrayrn (rn [0] => Arrayrn (rn [tid] => 17rn [pid] => 50rn [mid] => 8rn [topid] => 3rn [upid] => 3rn )rnrn [1] => Arrayrn (rn [tid] => 18rn [pid] => 50rn [mid] => 8rn [topid] => 3rn [upid] => 3rn )rnrn [2] => Arrayrn (rn [tid] => 19rn [pid] => 50rn [mid] => 8rn [topid] => 3rn [upid] => 3rn )rnrnrn )rnrn )rnrn [2] => Arrayrn (rn [tid] => 5rn [pid] => 2rn [mid] => 3rn [topid] => 0rn [upid] => 0rn [exmid] => 0rn [larray] => Arrayrn (rn [0] => Arrayrn (rn [tid] => 11rn [pid] => 50rn [mid] => 3rn [topid] => 5rn [upid] => 5rn )rnrn [1] => Arrayrn (rn [tid] => 12rn [pid] => 50rn [mid] => 3rn [topid] => 5rn [upid] => 5rn )rnrn [2] => Arrayrn (rn [tid] => 22rn [pid] => 50rn [mid] => 8rn [topid] => 5rn [upid] => 5rn )rnrn )rnrn )rnrn [3] => Arrayrn (rn [tid] => 50rn [pid] => 4rn [mid] => 8rn [topid] => 0rn [upid] => 0rn [larray] => rn )rnrn rnrn)rnrn[/code]rnrn最终结果如下:按照PID从小到大重新排列了一次。rn[code=php]rnArrayrn(rn [0] => Arrayrn (rn [pid] => 0rn [title] => 首页rn [path] => indexrn [current] => 0rn [link] => /index.phprn )rnrn [1] => Arrayrn (rn [tid] => 5rn [pid] => 2rn [mid] => 3rn [topid] => 0rn [upid] => 0rn [exmid] => 0rn [larray] => Arrayrn (rn [0] => Arrayrn (rn [tid] => 11rn [pid] => 50rn [mid] => 3rn [topid] => 5rn [upid] => 5rn )rnrn [1] => Arrayrn (rn [tid] => 12rn [pid] => 50rn [mid] => 3rn [topid] => 5rn [upid] => 5rn )rnrn [2] => Arrayrn (rn [tid] => 22rn [pid] => 50rn [mid] => 8rn [topid] => 5rn [upid] => 5rn )rnrn )rnrn )rnrnrn [2] => Arrayrn (rn [tid] => 50rn [pid] => 4rn [mid] => 8rn [topid] => 0rn [upid] => 0rn [larray] => rn )rnrn [3] => Arrayrn (rn [tid] => 3rn [pid] => 8rn [mid] => 8rn [topid] => 0rn [upid] => 0rn [exmid] => 0rn [linkid] => 2rn [gotoid] => 17rn [lng] => cnrn [larray] => Arrayrn (rn [0] => Arrayrn (rn [tid] => 17rn [pid] => 50rn [mid] => 8rn [topid] => 3rn [upid] => 3rn )rnrn [1] => Arrayrn (rn [tid] => 18rn [pid] => 50rn [mid] => 8rn [topid] => 3rn [upid] => 3rn )rnrn [2] => Arrayrn (rn [tid] => 19rn [pid] => 50rn [mid] => 8rn [topid] => 3rn [upid] => 3rn )rnrnrn )rnrn )rnrn rnrn)rnrn[/code]rn

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