How to group subarrays by a column value?

arrays, grouping, php

Solution

There is no native one, just use a loop.

$result = array();
foreach ($data as $element) {
    $result[$element['id']][] = $element;
}

Problem

I have the following array ``` Array ( [0] => Array ( [id] => 96 [shipping_no] => 212755-1 [part_no] => reterty [description] => tyrfyt [packaging_type] => PC ) [1] => Array ( [id] => 96 [shipping_no] => 212755-1 [part_no] => dftgtryh [description] => dfhgfyh [packaging_type] => PC ) [2] => Array ( [id] => 97 [shipping_no] => 212755-2 [part_no] => ZeoDark [description] => s%c%s%c%s [packaging_type] => PC ) ) ``` How can I group the array by `id`? Is there any native php functions are available to do this? While this approach works, I want to do this using a `foreach`, since with the above I will get duplicate items, which I'm trying to avoid? On the above example `id` have 2 items, so its need to be inside of the `id`

Original source