PHP multi-dimensional array deduplication (deduplication for any key value) - uniqueness of two-dimensional array - time complexity ~On

WBOY
Release: 2016-07-29 09:15:04
Original
983 people have browsed it

Take a two-dimensional array as an example to illustrate the deduplication of arbitrary key values. The time complexity is ~O(n), using only one foreach loop:

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 "
"; ?>
Copy after login

Output result:

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 ) )

Development example: Coupon deduplication (use offer amount-order amountfields cannot be repeatedremove duplicates)

Requirements: Discount amount For coupons with the same amount as the order amount, only one coupon is required to be displayed for the user to choose, and the one with the fastest expiration is displayed:

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'] <= $today) && ($today <= $v['usableEndTime'])) { //先确定优惠券的可用日期
                $keyOut = $v['couponAmount'] . "-" . $v['orderAmount']; 
                //提取内部一维数组的key(couponAmount orderAmount)作为外部数组的键
                if (array_key_exists($keyOut, $arrWish)) {
                    //展现最先到期的优惠券
                    if (intval($arrWish[$keyOut]['usableEndTime']) > intval($v['usableEndTime'])) {
                        $arrWish[$keyOut] = $v;  //如果原来数组中结束时间大的话,就交换值
                    }
                    continue;
                }
                $arrWish[$keyOut] = $v;  //实现二维数组唯一性
            }
            continue;
        }
        return $arrWish;
        }
        $arrWant = getArrayUniqueByKey($arrCoupon);
        print_r($arrWant);
?>
Copy after login

Output result:

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

The above introduces the deduplication of multi-dimensional arrays in PHP (deduplication for any key value) - the only thing about two-dimensional arrays - time complexity ~ On, including aspects of the content. I hope friends who are interested in PHP tutorials will have it. helped.

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!