0

我有回扣系统。用户将通过输入型号、序列号、经销商名称和购买日期来检查他们的产品是否有资格获得折扣。

如果产品符合条件,则将这些值添加到会话数组中。

array(4) { 

[0]=> array(6) { ["id"]=> string(0) "" ["model"]=> string(7) "CP05G10" ["serial"]=> string(9) "ABCK50786" ["pdate"]=> string(10) "2013-05-18" ["amt"]=> string(2) "25" ["dealer"]=> string(8) "79163500" } 

[1]=> array(6) { ["id"]=> string(0) "" ["model"]=> string(7) "EP24G33" ["serial"]=> string(9) "AAMK01127" ["pdate"]=> string(10) "2013-05-18" ["amt"]=> string(2) "25" ["dealer"]=> string(8) "79163500" } 

[2]=> array(6) { ["id"]=> string(0) "" ["model"]=> string(7) "CP05G10" ["serial"]=> string(9) "ABCK50786" ["pdate"]=> string(10) "2013-05-17" ["amt"]=> string(2) "25" ["dealer"]=> string(8) "79163500" } 

[3]=> array(6) { ["id"]=> string(0) "" ["model"]=> string(7) "EP24G33" ["serial"]=> string(9) "AAMK01127" ["pdate"]=> string(10) "2013-05-17" ["amt"]=> string(2) "25" ["dealer"]=> string(8) "79163500" } 

}  

我遇到的问题是用户添加了相同的型号和序列号,但购买日期或经销商不同。这会在系统中创建重复项。

我需要的是根据型号和序列号删除重复项。

我已经实施了这个建议:

    //echo $amount;
        if ($amount != ''){
            $rebate[] = $tmprebate = array("id" => '', "model" => $model_number, "serial" => $serial_number, "pdate" => $purchase_date, "amt" => $amount, "dealer" => $dealerid);
        }else{
            $noRebate[] = $tmprebate = array("id" => '', "model" => $model_number, "serial" => $serial_number, "pdate" => $purchase_date);
        }

    if( isset($_SESSION['rebate']) ){
       foreach($_SESSION['rebate'] as $session )
        { 
           if($session['serial'] != $serial_number && $session['model'] != $model_number){
               $_SESSION['rebate'] = $rebate;
               $_SESSION['noRebate'] = $noRebate;
            }
        }
    }else{
        $_SESSION['rebate']   = $rebate;
        $_SESSION['noRebate'] = $noRebate;
    }

我重新实现了建议的代码,一切正常。

        // check to see if model and serial already exist in Session
        if( isset($_SESSION['rebate'])){

           $match_rebate = 0;
           foreach($_SESSION['rebate'] as $session )
            {  
               if($session['serial'] == $serial_number && $session['model'] == $model_number){ // if session matches what is being submitted
                   $match_rebate = 1;
                }
            }
        }else{
            $_SESSION['rebate']   = $rebate;
            $_SESSION['noRebate'] = $noRebate;
        }

        if($match_rebate == 0)
        {
          $_SESSION['rebate']   = $rebate;
          $_SESSION['noRebate'] = $noRebate;
        } 
4

1 回答 1

1

不要根据型号和序列号删除重复项,而是在创建会话之前检查会话数组中是否存在型号和序列号。如果找到,您可以避免创建会话,或者创建一个。调整你的代码

if ($amount != ''){
        $rebate[] = $tmprebate = array("id" => '', "model" => $model_number, "serial" => $serial_number, "pdate" => $purchase_date, "amt" => $amount, "dealer" => $dealerid);
    }else{
        $noRebate[] = $tmprebate = array("id" => '', "model" => $model_number, "serial" => $serial_number, "pdate" => $purchase_date);
    }

if( isset($_SESSION['rebate'])){

   $match_rebate = 0;
   foreach($_SESSION['rebate'] as $session )
    {  
       if($session['serial'] == $serial_number && $session['model'] == $model_number){
           $match_rebate = 1;
        }
    }
}else{
    $_SESSION['rebate']   = $rebate;
}
if( isset($_SESSION['norebate'])){

   $match_norebate = 0;
   foreach($_SESSION['norebate'] as $session2 )
    {  
       if($session2['serial'] == $serial_number && $session2['model'] == $model_number){
           $match_norebate = 1;
        }
    }
}else{
    $_SESSION['noRebate'] = $noRebate;
}

if($match_rebate == 0)
{
  array_push($_SESSION['rebate'],$rebate);
}    

if($match_norebate == 0)
{
  array_push($_SESSION['norebate'],$noRebate);
}
于 2013-05-18T10:51:35.857 回答