Ale*_*Per 2 arrays sorting collections laravel laravel-5.1
我用代码创建了一个包含集合的数组:
$hours = Voucher::where('created_at','>=', Carbon::now()->startOfMonth())->get()->groupBy(function($item)
{
return $item->created_at->format('H');
});
dd($hours);
Run Code Online (Sandbox Code Playgroud)
所以我得到这样的数据:
SO 键是小时 (00-23)。
我如何安排它从 00、01、02、03 开始......
https://laravel.com/docs/5.6/collections#method-sortkeys
$collection = collect([
'id' => 22345,
'first' => 'John',
'last' => 'Doe',
]);
$sorted = $collection->sortKeys();
Run Code Online (Sandbox Code Playgroud)
5.1 更新
由于您使用的是 Laravel 5.1 并且没有 sortKeys 方法,因此您可以在对集合进行分组之前对其进行排序,这应该会导致键的顺序正确。
$collection->sort(function($a, $b) {
return strcmp($a->created_at->format('H'), $b->created_at->format('H'));
})->groupBy(function($item) {
return $item->created_at->format('H');
});
Run Code Online (Sandbox Code Playgroud)