I have an array:
[['a', 1], ['b', 1], ['c',2], ['d',2]]
How can I group the subarrays based on the second column value like this:
[[['a', 1], ['b', 1]], [['c',2], ['d',2]]]
I have an idea to solve this with a foreach, but it may have a way with the built-in functions?
Sketch with foreach:
$in = [['a', 1], ['b', 1], ['c',2], ['d',2]];
$out = [];
foreach($in as $i) {
$out[$i[1]][] = $i;
}
You more or less have what I would have arrived at for a solution.
As long as the data that you wish to use as an index will always be in the same position in the array, you will be able to hardcode the index as you have done in the collector array.
If not, I would think about moving to an associative array and assigning a name to the value that you want to use as an index when creating the array.