PHP二位数组按照数组的某个字段值排序

不多废话 直接代码

/**
* @name 排序 按照数组的某个字段值排序
* @param $array 排序数组 $field 排序字段 $direction 排序顺序
* @author wanglisong@aldwx.com
* @date 2017-03-09
*/

private function sort_array($array,$field,$direction) {

if($direction == 'desc') {
$direction = 'SORT_DESC';
}
if($direction == 'asc') {
$direction = 'SORT_ASC';
}
$sort = array(
'direction' => $direction, //排序顺序标志 SORT_DESC 降序;SORT_ASC 升序
'field' => $field, //排序字段
);
$arrSort = array();
foreach($array as $key => $value) {
foreach($value as $k => $v) {
$arrSort[$k][$key] = $v;
}
}

if($sort['direction']){
array_multisort($arrSort[$sort['field']], constant($sort['direction']), $array);
}

return $array;
}

posted @ 2017-05-06 18:20  King-  阅读(804)  评论(0编辑  收藏