How to group subarrays by a column value?

前端 未结 18 2037
一生所求
一生所求 2020-11-22 10:43

I have the following array

Array
(
    [0] => Array
        (
            [id] => 96
            [shipping_no] => 212755-1
            [part_no] =&         


        
18条回答
  •  半阙折子戏
    2020-11-22 11:07

    Consume and cache the column value that you want to group by, then push the remaining data as a new subarray of the group you have created in the the result.

    function array_group(array $data, $by_column)
    {
        $result = [];
        foreach ($data as $item) {
            $column = $item[$by_column];
            unset($item[$by_column]);
            $result[$column][] = $item;
        }
        return $result;
    }
    

提交回复
热议问题