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

时间:2021-11-22 17:26:00

不多废话 直接代码

/**
* @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;
}