我有一个回扣系统。用户将通过输入型号、序列号、经销商名称和购买日期来检查他们的产品是否符合折扣资格。
如果产品符合条件,则将值添加到会话数组中。
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;
}
在创建会话之前,请检查会话数组中是否存在该型号和序列号,而不是根据型号和序列号删除duplicate。如果找到,您可以避免创建会话,或者创建一个会话。调整代码
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);
}