php 二维数组去重

         $list=array(
            0=>array('trace_id'=>1,'trans_card_num'=>'001','total_fee'=>1),
            1=>array('trace_id'=>2,'trans_card_num'=>'002','total_fee'=>2),
            2=>array('trace_id'=>3,'trans_card_num'=>'003','total_fee'=>3),
            3=>array('trace_id'=>4,'trans_card_num'=>'004','total_fee'=>4),
            4=>array('trace_id'=>5,'trans_card_num'=>'005','total_fee'=>5),
            5=>array('trace_id'=>6,'trans_card_num'=>'005','total_fee'=>6),
            6=>array('trace_id'=>7,'trans_card_num'=>'005','total_fee'=>7),
            7=>array('trace_id'=>8,'trans_card_num'=>'005','total_fee'=>8),
        );
        $ar=array();
        foreach($list as $v){
            if(empty($ar) || !in_array($v,$ar)){
                $ar[$v['trans_card_num']][$v['trace_id']]=$v;
            }else{
                foreach($ar as $ka=>$va){
                    if($ka==$v['trans_card_num']){
                        $ar[$ka][$v['trace_id']]=$v;
                    }
                }
            }
        }
        foreach($ar as $k=>$v){
            /*
            if(is_array($v)){
                foreach($v as $key=>$val){
                    if(is_array($val)){
                        $val['num'] = count($v);
                    }
                    $v[$key] = $val;
                }
                $ar[$k] = $v;
            }*/
            $sum = 0 ;
            foreach($v as $key=>$val){
                if(is_array($val)){
                    $val['num'] = count($v);
                    if(isset($val['total_fee'])){
                        $sum += $val['total_fee'];
                    }
                }
                $v[$key] = $val;
            }
            foreach($v as $key=>$val){
                if(is_array($val)){
                    $val['total_price'] = $sum;
                }
                $v[$key] = $val;
            }

            $ar[$k] = $v;

        }
        $item = array();
        foreach($ar as $k=>$v){
            if(is_array($v)){
                $i=0;
                $item_arr = array();
                foreach($v as $key=>$val){
                    $item_arr[$i] = $val;
                    $i++;
                }
                $item[] = $item_arr;
            }
        }
        $foo = array();
        foreach($item as $k=>$v){
            if(is_array($v)){
                $foo[] = $v[0];
            }
        }
        
        
        echo '<pre>';
        print_r($foo);

        echo '</pre>';



输出的结果是

      

Array
(
    [0] => Array
        (
            [trace_id] => 1
            [trans_card_num] => 001
            [total_fee] => 1
            [num] => 1
            [total_price] => 1
        )

    [1] => Array
        (
            [trace_id] => 2
            [trans_card_num] => 002
            [total_fee] => 2
            [num] => 1
            [total_price] => 2
        )

    [2] => Array
        (
            [trace_id] => 3
            [trans_card_num] => 003
            [total_fee] => 3
            [num] => 1
            [total_price] => 3
        )

    [3] => Array
        (
            [trace_id] => 4
            [trans_card_num] => 004
            [total_fee] => 4
            [num] => 1
            [total_price] => 4
        )

    [4] => Array
        (
            [trace_id] => 5
            [trans_card_num] => 005
            [total_fee] => 5
            [num] => 4
            [total_price] => 26
        )

)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值