过滤和排序Laravel集合
我有这个收藏:过滤和排序Laravel集合,laravel,laravel-5,laravel-collection,Laravel,Laravel 5,Laravel Collection,我有这个收藏: 'subscription_1' => [ [ 'name' => 'test', 'date' => '2020-12-10' ], [ 'name' => 'another test' 'date' => '2020-12-10' ], [
'subscription_1' => [
[
'name' => 'test',
'date' => '2020-12-10'
],
[
'name' => 'another test'
'date' => '2020-12-10'
],
[
'name' => 'another test'
'date' => '2020-12-11'
],
[
'name' => 'another test'
'date' => '2020-12-11'
]
],
'subscription_5' => [
[
'name' => 'test',
'date' => '2020-03-04'
],
[
'name' => 'another test'
'date' => '2020-03-04'
],
[
'name' => 'another test'
'date' => '2020-08-06'
],
[
'name' => 'another test'
'date' => '2020-08-06'
]
]
]
订阅量很大
里面的项目是通知。可以有1到4个唯一的通知,每个通知可以重复两次,即最多8条记录
因此,单个订阅可能如下所示:
Notification 2020-08-06 15:13:05
Notification 2020-08-06 15:13:05
Notification 2020-09-04 12:05:03
Notification 2020-09-04 12:05:03
Notification 2020-10-05 14:03:05
Notification 2020-10-05 14:03:05
我想按日期对每个订阅的通知进行排序,然后提取$first
集合中的所有第一次通知,以及$second
集合中的所有第二次通知,依此类推,直到第四次
因此,在
$first
中,所有订阅和任何副本都应该有最早发送的通知。为什么不尝试使用collect()进行循环:
似乎您需要循环使用sortBy('date')并使用shift()获取元素,那么到目前为止您尝试了什么,您遇到了什么错误?您的描述令人困惑。首先给出一个从12月开始的例子,然后是3月,然后是8月。结果我看到了一月,二月,三月。我几乎不明白你想要实现什么。请先写一个输入,然后再写下所需的输出。这个任务很简单,你只需要更具体一些。@NikolayTraykov第二个块只是一个例子,说明了一个块可以容纳什么,而不是一个结果,可能还不够清楚,我会尝试重新编写它。
foreach($notifications as $id => $notification) {
$sorted = collect($notification)->sortBy('date');
$first[$id] = $sorted->shift();
$second[$id] = $sorted->shift();
$third[$id] = $sorted->shift();
$fourth[$id] = $sorted->shift();
}