处理二维数组, 按照某个字段合并重复的,其余字段中部分字段保留,部分相加

  //处理二维数组, 按照某个字段合并重复的,其余字段中部分字段保留,部分相加
public function deal_erwei_arr(){ $param = array( array ( 'hykno' => '2222222-CB', 'tcdk_fid' => '458B6D70-FB7F-47AE-8356-777777777777', 'totalamount' => '0', 'offsetamount' => '90.0', 'postamount' => '0', 'hysyamout' => '10.0', 'protypeid' => '1', ), array ( 'hykno' => '2222222-CB', 'tcdk_fid' => '458B6D70-FB7F-47AE-8356-777777777777', 'totalamount' => '0', // 需要支付的钱 'offsetamount' => '5.0', // 抵扣分类1下5.0份; 'postamount' => '0', // 运费 'hysyamout' => '5.0', 'protypeid' => '1', ), array ( 'hykno' => '2222222-CB', 'tcdk_fid' => '458B6D70-FB7F-47AE-8356-777777777777', 'totalamount' => '0', // 需要支付的钱 'offsetamount' => '5.0', // 抵扣分类1下5.0份; 'postamount' => '0', // 运费 'hysyamout' => '5.0', 'protypeid' => '7', ), array ( 'hykno' => '2222222-CB', 'tcdk_fid' => '458B6D70-FB7F-47AE-8356-777777777777', 'totalamount' => '0', // 需要支付的钱 'offsetamount' => '5.0', // 抵扣分类1下5.0份; 'postamount' => '0', // 运费 'hysyamout' => '5.0', 'protypeid' => '3', ) ); $u = array(); foreach ($param as $k => $v){ if (!in_array($v['protypeid'],$u)){ $u[] = $v['protypeid']; } } $data = array(); foreach ($u as $uk => $uv) { foreach ($param as $pk => $pv) { if ($pv['protypeid'] == $uv) { $data[$uv]['hykno'] = $pv['hykno']; $data[$uv]['totalamount'] += $pv['totalamount']; $data[$uv]['offsetamount'] += $pv['offsetamount']; $data[$uv]['postamount'] += $pv['postamount']; $data[$uv]['hysyamout'] += $pv['hysyamout']; $data[$uv]['protypeid'] = $pv['protypeid']; } } } $data = array_values($data); dump($data); }

 

posted @ 2018-12-17 16:59  盘思动  阅读(681)  评论(1编辑  收藏  举报