多个二维数组根据相等字段合并到新数组,不等的也增加到新数组

就如标题介绍的,如果我们有五个二维数组,但是字段又不相同,只有一个相同的sid,根据sid,把字段赋值到一个新的表里,如果没有相等,就向下加一个下标赋值过去,不多说,看代码:

1.先把五个二维数组合并

$data= array_merge($list,$list1,$list2,$list3,$list4);

得到一个新的二维数组:

$data[0]['clothing']='3';
        $data[0]['sid']='402';
        $data[0]['after_type']='3';
        $data[0]['order_type']='3';
        $data[0]['bid']='104';
        $data[0]['did']='16';

        $data[1]['clothing']='56';
        $data[1]['sid']='403';
        $data[1]['after_type']='3';
        $data[1]['order_type']='3';
        $data[1]['bid']='105';
        $data[1]['did']='15';

        $data[2]['functional_finishing']='567';
        $data[2]['sid']='402';
        $data[2]['after_type']='3';
        $data[2]['order_type']='3';
        $data[2]['bid']='104';
        $data[2]['did']='16';

        $data[3]['functional_finishing']='78';
        $data[3]['sid']='403';
        $data[3]['after_type']='3';
        $data[3]['order_type']='3';
        $data[3]['bid']='104';
        $data[3]['did']='16';

        $data[4]['functional_finishing']='89';
        $data[4]['sid']='123';
        $data[4]['after_type']='3';
        $data[4]['order_type']='3';
        $data[4]['bid']='104';
        $data[4]['did']='16';

        $data[5]['clothing_function']='78';
        $data[5]['sid']='402';
        $data[5]['after_type']='3';
        $data[5]['order_type']='3';
        $data[5]['bid']='104';
        $data[5]['did']='16';

可以看到,里面的字段并不是相同的,我们要根据sid操作

  $newdata = [];
        foreach($data as $k=>$v){
            if(!isset($newdata[$v['sid']])){//不等就完全赋值过去
                $newdata[$v['sid']][] = $v;
            }else{//相等就把不同的字段赋值过去$newdata[$v['sid']][0]['garment'] += $v['garment'];
                $newdata[$v['sid']][0]['garment_function'] += $v['garment_function'];
                $newdata[$v['sid']][0]['clothing'] += $v['clothing'];
                $newdata[$v['sid']][0]['functional_finishing'] += $v['functional_finishing'];
                $newdata[$v['sid']][0]['clothing_function'] += $v['clothing_function'];
            }
        }

然后的到一个三维数组

array(3) {
  [402] => array(1) {
    [0] => array(10) {
      ["clothing"] => int(3)
      ["sid"] => string(3) "402"
      ["after_type"] => string(1) "3"
      ["order_type"] => string(1) "3"
      ["bid"] => string(3) "104"
      ["did"] => string(2) "16"
      ["garment"] => int(0)
      ["garment_function"] => int(0)
      ["functional_finishing"] => int(567)
      ["clothing_function"] => int(78)
    }
  }
  [403] => array(1) {
    [0] => array(10) {
      ["clothing"] => int(56)
      ["sid"] => string(3) "403"
      ["after_type"] => string(1) "3"
      ["order_type"] => string(1) "3"
      ["bid"] => string(3) "105"
      ["did"] => string(2) "15"
      ["garment"] => int(0)
      ["garment_function"] => int(0)
      ["functional_finishing"] => int(78)
      ["clothing_function"] => int(0)
    }
  }
  [123] => array(1) {
    [0] => array(6) {
      ["functional_finishing"] => string(2) "89"
      ["sid"] => string(3) "123"
      ["after_type"] => string(1) "3"
      ["order_type"] => string(1) "3"
      ["bid"] => string(3) "104"
      ["did"] => string(2) "16"
    }
  }
}

不急,我们转换为二维数组

$new_arr=[];
foreach ($newdata as $k=>$v){ $new_arr[]=$v[0]; }

好了,这就是多个二维数组依据条件赋值的操作,如果你也有闭着更好的办法,欢迎留言,不懂得地方也可以留言额

posted @ 2019-10-30 16:50  原来是个帅小伙  阅读(441)  评论(0编辑  收藏  举报