问题
I have the following array and I would like to be able to organize it through collection
$collection = Collection($total);
$filter = $collection->groupBy('date_general')->toArray();
[2017-11-14] => Array
(
[0] => Array
(
[status] => pending
[date_general] => 2017-11-14
[user_id] => 164
)
[1] => Array
(
[status] => pending
[date_general] => 2017-11-14
[user_id] => 112
)
)
Up to this point I already have the array organized by dates. Now within each date I need to organize it by user, that is to say that it is:
[2017-11-14] => Array
(
[164] => Array
(
[0] => Array
(
[status] => pending
[date_general] => 2017-11-14
[user_id] => 164
)
)
[112] => Array
(
[0] => Array
(
[status] => pending
[date_general] => 2017-11-14
[user_id] => 112
)
)
)
回答1:
You'll have to process each date group and individually group their contents.
$collection = collection($total)
->groupBy('date_general')
->map(function ($data) {
return collection($data)->groupBy('user_id')->toArray();
});
See also
- Cookbook > Collections
来源:https://stackoverflow.com/questions/47304495/organize-a-groupby-within-another-groupby-using-collection-in-cakephp