Laravel 8 Arr::sort() {#collection-method}
2021-07-19 10:19 更新
Arr::sort
函数根据数组的值对数组进行排序:
use Illuminate\Support\Arr;
$array = ['Desk', 'Table', 'Chair'];
$sorted = Arr::sort($array);
// ['Chair', 'Desk', 'Table']
你也可以根据给定闭包返回的结果对数组进行排序:
use Illuminate\Support\Arr;
$array = [
['name' => 'Desk'],
['name' => 'Table'],
['name' => 'Chair'],
];
$sorted = array_values(Arr::sort($array, function ($value) {
return $value['name'];
}));
/*
[
['name' => 'Chair'],
['name' => 'Desk'],
['name' => 'Table'],
]
*/
以上内容是否对您有帮助:
更多建议: