就如標題介紹的,若是咱們有五個二維數組,可是字段又不相同,只有一個相同的sid,根據sid,把字段賦值到一個新的表裏,若是沒有相等,就向下加一個下標賦值過去,很少說,看代碼:數組
1.先把五個二維數組合並spa
$data= array_merge($list,$list1,$list2,$list3,$list4);
獲得一個新的二維數組:code
$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操做blog
$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']; } }
而後的到一個三維數組string
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" } } }
不急,咱們轉換爲二維數組io
$new_arr=[];
foreach ($newdata as $k=>$v){ $new_arr[]=$v[0]; }
好了,這就是多個二維數組依據條件賦值的操做,若是你也有閉着更好的辦法,歡迎留言,不懂得地方也能夠留言額function