Skip to content
Advertisement

Laravel – Collections. How to separate array into 2 groups

I have created 2 collections(arrays) which each contain an array of 5 items/events.

JavaScript

Output of the above:

JavaScript

In my next loop, it simply goes through every item of the array 1 by 1. I need to split the 5 items into a further 2 groups:
group of 2
group of 3
so I can wrap a div around each group.

Current loop:

JavaScript

What I need:

JavaScript

Full code:

JavaScript

Advertisement

Answer

Assuming that you will allways have 5 items, a solution could be:

JavaScript

Or if want to avoid code duplication:

JavaScript

If you don’t know or not sure that you have 5, you may need to change the logic of the chunks/slice.

User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement