I have started working on some data analyising, and the data is really messy, so i have to convert timestamps to readable date and time and such..
I have tried standard group by but it was not successful because the array is nested and i have no idea how to approach.
Illuminate\Support\Collection Object
(
[items:protected] => Array
(
[0] => Array
(
[name] => Entity 1
[values] => Array
(
[0] => Array
(
[date] => 08.07.2019
[time] => 00:00
[counter] => 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
)
)
)
So what would be the correct way to group this by date so i could get one array for one date for example
date 08.07: time: 02:00 counter: 12
timer: 04:00
counter: 1
date 14.07: time: 05:00 counter: 1
time: 08:00
counter: 3
time: 01:00
counter: 6
Sorry for dump example of an array
I have tried something like:
$collection = collect($values);
$groupedBy = $collection->groupBy('date);
But that just doesn't work since it's a nested array.
Before grouping you could reduce the collection in order to have only the items' values. After that you can simply use group by date and time:
$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']);
});
where Collection
is the imported Illuminate\\Support\\Collection
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.