Organize a groupBy within another groupBy using Collection in CakePHP

可紊 提交于 2019-12-24 07:49:59

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!