collect($vars)->groupBy() returns in the format
[
'catgeory' => [
['id' => 1, 'name' => 'name1', 'type' => 'category'],
['id' => 2, 'name' => 'name4', 'type' => 'category'],
]
'name' => ['id' => 3, 'name' => 'name2', 'type' => 'name'],
'value' => ['id' => 4, 'name' => 'name3', 'type' => 'value']
]
What is the best way to change to like this, removing the grouping but preferrably using the collections itself
[
['id' => 1, 'name' => 'name1', 'type' => 'category'],
['id' => 2, 'name' => 'name4', 'type' => 'category'],
['id' => 3, 'name' => 'name2', 'type' => 'name'],
['id' => 4, 'name' => 'name3', 'type' => 'value'],
]
Original array is this:
[
['id' => 3, 'name' => 'name2', 'type' => 'name'],
['id' => 1, 'name' => 'name1', 'type' => 'category'],
['id' => 2, 'name' => 'name4', 'type' => 'category'],
['id' => 4, 'name' => 'name3', 'type' => 'value'],
]
By using flatten() with a depth of one.
collect($vars)->groupBy('type')->flatten(1);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With