0

我正在尝试根据排名为对象定价。我的问题是,如果对象没有排名,它将进入下一层。这是我的代码示例:

switch ($amazonResult['SalesRank']) {
case ($amazonResult['SalesRank'] < 1 || trim($amazonResult['SalesRank'])===''|| !isset($amazonResult['SalesRank']) || $amazonResult['SalesRank']=== null):
    $Price=((float) $lowestAmazonPrice) *<some percent to pay>;
    $payPrice = round($Price, 0);  //to round the price up or down to the nearest $
    break; 
case ($amazonResult['SalesRank'] > 0 && $amazonResult['SalesRank'] <= 15000):
    $Price=((float) $lowestAmazonPrice) * <some percent to pay>;
    $payPrice = round($Price, 0);  //to round the price up or down to the nearest $
    break;
default:
    $Price=((float) $lowestAmazonPrice) * <some percent to pay>;
    $payPrice = round($Price, 0);  //to round the price up or down to the nearest $
    break;
}

如果排名为空、null 或 0,如何获取排名?

$amazonResult['SalesRank'] 可以为空,并且是需要在每种情况下进行比较的值。此变量从查询中提取,并在每次要为商品定价时运行

4

1 回答 1

0

尝试这个:

if(!isset($amazonResult['SalesRank']) || empty(trim($amazonResult['SalesRank'])) {
    // case if the variable is empty.
} else if($amazonResult['SalesRank'] < 1) {
    // some "valid" value
    $Price=((float) $lowestAmazonPrice) *<some percent to pay>;
    $payPrice = round($Price, 0);  //to round the price up or down to the nearest $
    break;
} else if($amazonResult['SalesRank'] > 0 && $amazonResult['SalesRank'] <= 15000) {
    $Price=((float) $lowestAmazonPrice) * <some percent to pay>;
    $payPrice = round($Price, 0);  //to round the price up or down to the nearest $
    break;
} else {
    $Price=((float) $lowestAmazonPrice) * <some percent to pay>;
    $payPrice = round($Price, 0);  //to round the price up or down to the nearest $
    break;
}

之后的值case应该是常量,以便您可以将其与中的变量进行比较switch

您的问题是您的 case 构造将返回 true 或 false ,这可能不等于$amazonResult['SalesRank'].

于 2012-05-18T16:27:15.443 回答