按嵌套的laravel集合分组

时间:2019-07-08 12:09:46

标签: php laravel

我已经开始进行一些数据分析,并且数据确实很乱,所以我必须将时间戳转换为可读的日期和时间,等等。

我尝试过标准分组方式,但由于数组是嵌套的,我不知道如何处理,因此未成功。

Illuminate \ Support \ Collection对象 (     [item:protected] =>数组         (             [0] =>数组                 (                     [名称] =>实体1                     [值] =>数组                         (                             [0] =>数组                                 (                                     [日期] => 08.07.2019                                     [时间] => 00:00                                     [计数器] => 1                                 )

                        [1] => Array
                            (
                                [date] => 08.07.2019
                                [time] => 01:00
                                [counter] => 3
                            )

                        [167] => Array
                            (
                                [date] => 14.07.2019
                                [time] => 23:00
                                [counter] => 0
                            )

                    )

            )
        [1] => Array
            (
                [name] => Entity 2
                [values] => Array
                    (
                        [0] => Array
                            (
                                [date] => 08.07.2019
                                [time] => 00:00
                                [counter] => 0
                            )

                        [1] => Array
                            (
                                [date] => 08.07.2019
                                [time] => 01:00
                                [counter] => 4
                            )

                        [2] => Array
                            (
                                [date] => 08.07.2019
                                [time] => 02:00
                                [counter] => 12
                            )

                        [3] => Array
                            (
                                [date] => 09.07.2019
                                [time] => 03:00
                                [counter] => 0
                            )

                        [4] => Array
                            (
                                [date] => 10.07.2019
                                [time] => 04:00
                                [counter] => 4
                            )

                        [5] => Array
                            (
                                [date] => 11.07.2019
                                [time] => 05:00
                                [counter] => 17
                            )

                    )

            )

那么按日期对它进行分组的正确方法是什么,这样我就可以为xample的一个日期获取一个数组

日期08.07:     时间:02:00     柜台:12

timer: 04:00
counter: 1

日期14.07:     时间:05:00     计数器:1

time: 08:00
counter: 3

time: 01:00
counter: 6

很抱歉提供阵列转储示例

我尝试过类似的事情:

$ collection = collect($ values); $ groupedBy = $ collection-> groupBy('date);

但这只是无效,因为它是一个嵌套数组。

1 个答案:

答案 0 :(得分:1)

在分组之前,您可以减少集合以仅具有项目的值。之后,您可以简单地按日期和时间分组:

      A   B
0  Adam  90
1  Adam  12

其中$grouped = $collection ->reduce(function(Collection $carry, $item) { return $carry->merge($item['values']); }, new Collection()) ->groupBy(function($item) { return sprintf('%s %s', $item['date'], $item['time']); }); 是导入的Collection