How to group subarrays by a column value?

前端 未结 18 1974
一生所求
一生所求 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:25

    You can try the following:

    $group = array();
    
    foreach ( $array as $value ) {
        $group[$value['id']][] = $value;
    }
    
    var_dump($group);
    

    Output:

    array
      96 => 
        array
          0 => 
            array
              'id' => int 96
              'shipping_no' => string '212755-1' (length=8)
              'part_no' => string 'reterty' (length=7)
              'description' => string 'tyrfyt' (length=6)
              'packaging_type' => string 'PC' (length=2)
          1 => 
            array
              'id' => int 96
              'shipping_no' => string '212755-1' (length=8)
              'part_no' => string 'dftgtryh' (length=8)
              'description' => string 'dfhgfyh' (length=7)
              'packaging_type' => string 'PC' (length=2)
      97 => 
        array
          0 => 
            array
              'id' => int 97
              'shipping_no' => string '212755-2' (length=8)
              'part_no' => string 'ZeoDark' (length=7)
              'description' => string 's%c%s%c%s' (length=9)
              'packaging_type' => string 'PC' (length=2)
    

提交回复
热议问题