How to group subarrays by a column value?

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

    function groupeByPHP($array,$indexUnique,$assoGroup,$keepInOne){
    $retour = array();
    $id = $array[0][$indexUnique];
    foreach ($keepInOne as $keep){
        $retour[$id][$keep] = $array[0][$keep];
    }
    foreach ($assoGroup as $cle=>$arrayKey){
        $arrayGrouped = array();
            foreach ($array as $data){
                if($data[$indexUnique] != $id){
                    $id = $data[$indexUnique];
                    foreach ($keepInOne as $keep){
                        $retour[$id][$keep] = $data[$keep];
                    }
                }
                foreach ($arrayKey as $val){
                    $arrayGrouped[$val] = $data[$val];
                }
                $retour[$id][$cle][] = $arrayGrouped;
                $retour[$id][$cle] = array_unique($retour[$id][$cle],SORT_REGULAR);
            }
    }
    return  $retour;
    }
    

    Try this function

    groupeByPHP($yourArray,'id',array('desc'=>array('part_no','packaging_type')),array('id','shipping_no')) 
    

提交回复
热议问题