合并数组中重复的值 ,并进行加减计算
1 <?php 2 //合并数组中重复的值 ,并进行加减计算 3 $arrs = Array ( 4 Array ( 'rname' => 'name', 'm1'=> 12 ,'m2'=> 33 ) , 5 Array ( 'rname' => 'name', 'm1' => 23,'m2'=> 44 ) , 6 Array ( 'rname' => 'ccc', 'm1' => 11,'m2'=> 22 ) 7 ); 8 //这里假设你要处理的数组是 $arrs 9 $tmpval=array(); 10 $data=array(); 11 $i=0; 12 //遍历原数据 13 foreach($arrs as $key=>$arr){ 14 if(!empty($tmpval) && array_key_exists($arr['rname'],$tmpval)){ 15 //存在 16 $key=$tmpval[$arr['rname']]; 17 //$arrsize=explode(',',$data[$key]['m1']); 18 //if(!in_array($arr['m1'],$arrsize)) $data[$key]['m1'].=','.$arr['m1'];//合并数组的值 19 //if(!in_array($arr['m1'],$arrsize)) { 20 $data[$key]['m1']+=$arr['m1']; 21 $data[$key]['m2']+=$arr['m2']; 22 //} 23 }else{ 24 //不存在 25 $tmpval[$arr['rname']]=$i; 26 $data[$i++]=$arr; 27 } 28 } 29 print_r($data); //输出结果($data)看看
https://zhidao.baidu.com/question/134248441793132365.html