1. 程式人生 > >php多維陣列的去重(針對任意的鍵值進行去重)--二維陣列的唯一--時間複雜度~O(n)

php多維陣列的去重(針對任意的鍵值進行去重)--二維陣列的唯一--時間複雜度~O(n)

以二維陣列為例,來說明針對任意鍵值的去重,時間複雜度為~O(n),只用一個foreach迴圈:

<?php 
$arr = array(
                     '0'=>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:<br>");
               print_r($arr_wish);
               echo "<br/>";

?>

輸出結果:

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

開發例項:優惠券去重(以優惠金額-訂單金額欄位不能重複去除重複項)

要求:優惠金額和訂單金額都一樣的優惠券要求只展示一張給使用者選擇,並且展示最快到期的那張:

 

<?php
       $arrCoupon = array(
            '0'=>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);
?>

輸出結果:

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