How to divide an array to arrays?

We have an array where we have 10 objects (pages). Each object (page) has a header attribute. Some unique, some are not. We need to break this array so that we will have so much arrays as there are different page header attributes. Milk, Sand, Bread etc are page header attributes.

Now:
—yaml
Array[]:
Milk
Bread
Sand
Car
Milk
Sand


We need:
---yaml
Array[]:
   Milk
   Milk
Array[]:
   Sand
   Sand
Array[]:
   Car
Array[]: 
   Bread

etc.

We already know how to exclude duplicates. But how to divide them?

Try filter pages by taxonomy
https://learn.getgrav.org/content/collections#taxonomy-collections