欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  后端开发

php多维数组的去重(针对任意的键值进行去重)--二维数组的唯一--时间复杂度~On

程序员文章站 2022-04-27 21:30:20
...
以二维数组为例,来说明针对任意键值的去重,时间复杂度为~O(n),只用一个foreach循环
array(
                            'name'=>'james',
                            'age'=>30,
                              ),
                     '1'=>array(
                            'name'=>'susu',
                            'age'=>26,
                             ),
                     '2'=>array(
                            'name'=>'james',
                            'age'=>30,
                              ),
                     'new'=>array(
                            'name'=>'kube',
                            'age'=>37,
                              ),
                     'list'=>array(
                            'name'=>'kube',
                            'age'=>27,
                              ),
                    );
 /*针对任意键值来进行去重*/
               function getArrayUniqueByKeys($arr)
               {
                   $arr_out =array();
                   foreach($arr as $k => $v)
                   {
                        $key_out = $v['name']."-".$v['age']; //提取内部一维数组的key(name age)作为外部数组的键

                        if(array_key_exists($key_out,$arr_out)){
                            continue;
                        }
                        else{
                             $arr_out[$key_out] = $arr[$k]; //以key_out作为外部数组的键
                             $arr_wish[$k] = $arr[$k];  //实现二维数组唯一性
                        }
                   }
                   return $arr_wish;
               }
               $arr_wish = getArrayUniqueByKeys($arr);
               printf("As for the arbitrarily key:
"); print_r($arr_wish); echo "
"; ?>

输出结果:

As for the arbitrarily key:
Array ( [0] => Array ( [name] => james [age] => 30 ) [1] => Array ( [name] => susu [age] => 26 ) [new] => Array ( [name] => kube [age] => 37 ) [list] => Array ( [name] => kube [age] => 27 ) )

开发实例:优惠券去重(以优惠金额-订单金额字段不能重复去除重复项)

要求:优惠金额和订单金额都一样的优惠券要求只展示一张给用户选择,并且展示最快到期的那张:

array(
                'couponCode' => '3033323852301056',
                'usableStartTime' => "1439740800",
                'usableEndTime'=>"1440798100",
                'couponAmount'  =>100,
                'orderAmount'=> 800,
                ),
            '1'=>array(
                'couponCode' => '3033323852301057',
                'usableStartTime' => "1439740800",
                'usableEndTime'=>"1440768100",
                'couponAmount'  =>100,
                'orderAmount'=> 800,
            ),
            '2'=>array(
                'couponCode' => '3033323852301058',
                'usableStartTime' => "1439740800",
                'usableEndTime'=>"1440788100",
                'couponAmount'  =>100,
                'orderAmount'=> 800,
            ),
            '3'=>array(
                'couponCode' => '3033323852301059',
                'usableStartTime' => "1439740800",
                'usableEndTime'=>"1440779100",
                'couponAmount'  =>200,
                'orderAmount'=> 800,
            ),
            '4'=>array(
                'couponCode' => '3033323852301060',
                'usableStartTime' => "1439740800",
                'usableEndTime'=>"1440758100",
                'couponAmount'  =>200,
                'orderAmount'=> 800,
            ),
            '5'=>array(
                'couponCode' => '3033323852301061',
                'usableStartTime' => "1439740800",
                'usableEndTime'=>"1440798100",
                'couponAmount'  =>200,
                'orderAmount'=> 800,
            ),
        );
        //print_r($arrCoupon);
        function getArrayUniqueByKey($arr)
        {
        $arrWish = array();
        $today = time();
        foreach ($arr as $k => $v) {
            if (($v['usableStartTime']  intval($v['usableEndTime'])) {
                        $arrWish[$keyOut] = $v;  //如果原来数组中结束时间大的话,就交换值
                    }
                    continue;
                }
                $arrWish[$keyOut] = $v;  //实现二维数组唯一性
            }
            continue;
        }
        return $arrWish;
        }
        $arrWant = getArrayUniqueByKey($arrCoupon);
        print_r($arrWant);
?>

输出结果:

Array( [100-800] =>Array ( [couponCode] => 3033323852301057 [usableStartTime] => 1439740800[usableEndTime] => 1440768100 [couponAmount] => 100 [orderAmount] =>800 ) [200-800]=> Array ( [couponCode] => 3033323852301060 [usableStartTime] =>1439740800 [usableEndTime] => 1440758100 [couponAmount] => 200 [orderAmount]=> 800 ) )

以上就介绍了php多维数组的去重(针对任意的键值进行去重)--二维数组的唯一--时间复杂度~On,包括了方面的内容,希望对PHP教程有兴趣的朋友有所帮助。