# yii2 ArrayHelper的多维数组排序函数multisort

public static void multisort ( &$array,$key, $direction = SORT_ASC,$sortFlag = SORT_REGULAR )

/**
* 我们对数组$arr按照id的大小进行降序排序 */$arr = [
0=>['id'=>1,'name'=>'abei'],
1=>['id'=>2,'name'=>'li'],
3=>['id'=>4,'name'=>'wb'],
4=>['id'=>3,'name'=>'world']
];

ArrayHelper::multisort($arr,'id',SORT_DESC); var_dump($arr);

//    获得结果为
array (size=4)
0 =>
array (size=2)
'id' => int 4
'name' => string 'wb' (length=2)
1 =>
array (size=2)
'id' => int 3
'name' => string 'world' (length=5)
2 =>
array (size=2)
'id' => int 2
'name' => string 'li' (length=2)
3 =>
array (size=2)
'id' => int 1
'name' => string 'abei' (length=4)

/**
* 我们对数组$arr按照id的大小进行降序排序，然后对name进行生序排序 */$arr = [
0=>['id'=>1,'name'=>'abei'],
1=>['id'=>1,'name'=>'li'],
3=>['id'=>1,'name'=>'wb'],
4=>['id'=>3,'name'=>'world']
];

ArrayHelper::multisort($arr,['id','name'],[SORT_DESC,SORT_ASC]); var_dump($arr);

//    获得结果为
array (size=4)
0 =>
array (size=2)
'id' => int 3
'name' => string 'world' (length=5)
1 =>
array (size=2)
'id' => int 1
'name' => string 'abei' (length=4)
2 =>
array (size=2)
'id' => int 1
'name' => string 'li' (length=2)
3 =>
array (size=2)
'id' => int 1
'name' => string 'wb' (length=2)

*/
$arr = [ 0=>['id'=>1,'name'=>'abei'], 1=>['id'=>1,'name'=>'li'], 3=>['id'=>1,'name'=>'wb'], 4=>['id'=>3,'name'=>'world'] ]; ArrayHelper::multisort($arr,function($item){ if($item['id'] == 1){
return 'w';
}else{
return 'b';
}
},SORT_DESC);
var_dump($arr); // 获得结果为 array (size=4) 0 => array (size=2) 'id' => int 1 'name' => string 'abei' (length=4) 1 => array (size=2) 'id' => int 1 'name' => string 'li' (length=2) 2 => array (size=2) 'id' => int 1 'name' => string 'wb' (length=2) 3 => array (size=2) 'id' => int 3 'name' => string 'world' (length=5) 第4个函数$sortFlag说明 $sortFlag共有6个值，详情可以参考 http://php.net/manual/en/function.sort.php， multisort将排序的值根据$sortFlag的设置转换类型后排序。

posted @ 2020-12-02 17:33  一个人的孤独自白  阅读(122)  评论(0编辑  收藏  举报