我有回扣系统。用户将通过输入型号、序列号、经销商名称和购买日期来检查他们的产品是否有资格获得折扣。
如果产品符合条件,则将这些值添加到会话数组中。
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;
}