折扣数组
$Discount=arrray(
0=>array('FromArea'=>0,'ToArea'=>0,'Master'=>0,'Slave'=>0),
1=>array('FromArea'=>0,'ToArea'=>10,'Master'=>5,'Slave'=>0),
2=>array('FromArea'=>5,'ToArea'=>0,'Master'=>0,'Slave'=>8),
3=>array('FromArea'=>0,'ToArea'=>0,'Master'=>1,'Slave'=>2),
4=>array('FromArea'=>0,'ToArea'=>1,'Master'=>7,'Slave'=>5),
...
)
我想根据功能的输入参数获得折扣金额。
如果!empty(参数),则在数组中检查
喜欢这个
function DiscountAmount($FromArea, $ToArea, $Master, $Slave){
foreach ($Discounts as $R) {
if (!empty($FromArea) && empty($ToArea) && empty($Master) && empty($Slave)) {
if ($R["FromArea"] == $FromArea)
return true;
} else if (!empty($FromArea) && !empty($ToArea) && empty($Master) && empty($Slave)) {
if ($R["FromArea"] == $FromArea && $R["ToArea"] == $ToArea)
return true;
} else if (!empty($FromArea) && !empty($ToArea) && !empty($Master) && empty($Slave)) {
if ($R["FromArea"] == $FromArea && $R["ToArea"] == $ToArea && $R["Master"] == $Master)
return true;
} else if (!empty($FromArea) && !empty($ToArea) && !empty($Master) && !empty($Slave)) {
if ($R["FromArea"] == $FromArea && $R["ToArea"] == $ToArea && $R["Master"] == $Master && $R["Slave"] == $Slave)
return true;
} else if (!empty($FromArea) && !empty($ToArea) && !empty($Master) && !empty($Slave)) {
if ($R["FromArea"] == $FromArea && $R["ToArea"] == $ToArea && $R["Master"] == $Master && $R["Slave"] == $Slave)
return true;
}
...
}
}
$amout=DiscountAmount(0, 0, 0, 0);
$amout=DiscountAmount(1, 0, 0, 0);
$amout=DiscountAmount(0, 1, 0, 0);
$amout=DiscountAmount(0, 0, 1, 0);
$amout=DiscountAmount(0, 0, 0, 1);
$amout=DiscountAmount(1, 1, 0, 0);
$amout=DiscountAmount(1, 0, 1, 0);
$amout=DiscountAmount(1, 0, 0, 1;
$amout=DiscountAmount(1, 1, 1, 0);
$amout=DiscountAmount(1, 1, 0, 1);
$amout=DiscountAmount(1, 1, 1, 0);
$amout=DiscountAmount(1, 1, 1, 1);
....
Online Demo 但是通过这种方式你必须检查非常情况。有没有更简单的解决方案呢?
答案 0 :(得分:2)
function DiscountAmount($FromArea, $ToArea, $Master, $Slave)
{
//This line's simply to save time: if all fields are empty, no need to check further
if(empty($FromArea) && empty($ToArea) && empty($Master) && empty($Slave))
return true;
foreach ($Discounts as $R)
if (equiv($FromArea, $R["FromArea"]) &&
equiv($ToArea, $R["ToArea" ]) &&
equiv($Master, $R["Master" ]) &&
equiv($Slave, $R["Slave" ]))
return true;
return false;
}
function equiv($Field,$OtherField)
{
if (empty($Field)) return true;
else return $Field == $OtherField;
}
这应该显着减少笨重。
答案 1 :(得分:2)
function DiscountAmount($FromArea = null, $ToArea = null, $Master = null, $Slave = null) {
global $Discounts; //pull discount array into the function
$argsCount = count(func_get_args()); //the number of arguments filled in
$checkArray = array(
'FromArea' => $FromArea, 'ToArea' => $ToArea, 'Master' => $Master, 'Slave' => $Slave
);
while (count($checkArray) < $argsCount) {
array_pop($checkArray);
}
foreach ($Discounts as $R) {
if ($checkArray == $R) { //checks if all key/value pairs are equal
return true;
}
}
return false;
}
答案 2 :(得分:2)
尝试使用正则表达式:
function DiscountAmount($FromArea = null, $ToArea = null, $Master = null, $Slave = null){
if(empty($FromArea) && empty($ToArea) && empty($Master) && empty($Slave)) return true;
global $Discounts;
$json = json_encode($Discounts);
$reg = "/\{";
if(!empty($FromArea)) $reg .= "(?:[^\}]*\"FromArea\":{$FromArea})";
if(!empty($ToArea)) $reg .= "(?:[^\}]*\"ToArea\":{$ToArea})";
if(!empty($Master)) $reg .= "(?:[^\}]*\"Master\":{$Master})";
if(!empty($Slave)) $reg .= "(?:[^\}]*\"Slave\":{$Slave})";
$reg .= "[^\}]*\}/";
return preg_match($reg, $json);
}
答案 3 :(得分:1)
我认为这将消除冗余:
function DiscountAmount($Parameters){
foreach ($Discounts as $R) {
foreach ($Parameters as $PK => $P) {
$CheckFlag = true;
if (!empty($P)) {
if ($R[$PK] != $P) {
$CheckFlag = false;
break;
}
}
}
if (!empty($CheckFlag)) { return true; }
}
return false;
}
根据问题的代码和接受的答案:
- 如果任何数组具有与输入值匹配的非空值,则该函数必须返回true
- 如果所有输入数组都为空,则函数必须返回true