是否有一种最佳方法将字符串应用于IF/Else链以外的变量



我正在从api中拉出变量字符串Rarity

然后,我需要根据收到的内容来缩短此字符串。因此,我正在使用一个大的if/else块:

if($rarity == "Common"){
    $rarestring = "(C)";
}else if($rarity == "Rare"){
    $rarestring = "(R)";
}else if($rarity == "Super Rare"){
    $rarestring = "(SR)";
}else if($rarity == "Holofoil Rare"){
    $rarestring = "(HFR)";
}else if($rarity == "Ultra Rare"){
    $rarestring = "(UR)";
}else if($rarity == "Ultimate Rare"){
    $rarestring = "(UtR)";
}else if($rarity == "Secret Rare"){
    $rarestring = "(ScR)";
}else if($rarity == "Ultra Secret Rare"){
    $rarestring = "(UScR)";
}else if($rarity == "Secret Ultra Rare"){
    $rarestring = "(SCuR)";
}else if($rarity == "Prismatic Secret Rare"){
    $rarestring = "(PScR)";
}else if($rarity == "Ghost Rare"){
    $rarestring = "(GR)";
}else if($rarity == "Parallel Rare"){
    $rarestring = "(PR)";
}else if($rarity == "Parallel Common"){
    $rarestring = "(PC)";
}else if($rarity == "Super Parallel Rare"){
    $rarestring = "(SPR)";
}else if($rarity == "Ultra Parallel Rare "){
    $rarestring = "(UPR)";
}else if($rarity == "Duel Terminal Parallel Common"){
    $rarestring = "(DPC)";
}else if($rarity == "Duel Terminal Rare Parallel Rare"){
    $rarestring = "(DRPR)";
}else if($rarity == "Duel Terminal Super Parallel Rare"){
    $rarestring = "(DSPR)";
}else if($rarity == "Duel Terminal Ultra Parallel Rare"){
    $rarestring = "(DUPR)";
}else if($rarity == "Duel Terminal Secret Parallel Rare"){
    $rarestring = "(DScPR)";
}else if($rarity == "Gold Rare"){
    $rarestring = "(GUR)";
}    

我不禁感到这不是最佳。我知道/其他通常不是那种表现强度的,但我觉得这可能是学习一些东西并可能做得更好的好机会。

我认为我可以将所有稀有物都放入一个数组中,但是我仍然遇到相同的问题,我需要比较它然后缩短。

是的,使用数组映射:

$options = [
  'Common' => '(C)',
  'Rare' => '(R)',
];
//...
if (!isset($options[$rarity])) {
  throw new Exception('Unknown rarity ' . $rarity);
}
$rarestring = $options[$rarity];

或者您可以使用交换机结构:

switch($rarity) {
  case 'Common': $rarestring = '(C)'; break;
  case 'Rare': $rarestring = '(R)'; break;
  default: throw new Exception('Unknown rarity ' . $rarity);
}

编辑:

对于那些可能读这本书的人是未来。与许多人会说的相反,数组总是更好,这两种方法(数组与开关(之间存在真正的区别,而其他方式是用代码编写的方式。

开关的行为非常类似于Elseifs的序列 - 它允许每种情况执行代码块,最后一个情况需要最多的时间才能达到,因为所有以前的情况都必须与针头进行比较(上面的情况(。时间复杂性o(n(。内存复杂性o(1(。

另一方面,数组仅允许用用例,当每种情况应回馈预定义值时。在PHP数组中,将实现为哈希表,而关键查找的时间为o(1(<T<在(。记忆复杂性为o(n(。

您可以做这样的事情,

$arr = [
    "Common"                             => "(C)",
    "Rare"                               => "(R)",
    "Super Rare"                         => "(SR)",
    "Holofoil Rare"                      => "(HFR)",
    "Ultra Rare"                         => "(UR)",
    "Ultimate Rare"                      => "(UtR)",
    "Secret Rare"                        => "(ScR)",
    "Ultra Secret Rare"                  => "(UScR)",
    "Secret Ultra Rare"                  => "(SCuR)",
    "Prismatic Secret Rare"              => "(PScR)",
    "Ghost Rare"                         => "(GR)",
    "Parallel Rare"                      => "(PR)",
    "Parallel Common"                    => "(PC)",
    "Super Parallel Rare"                => "(SPR)",
    "Ultra Parallel Rare "               => "(UPR)",
    "Duel Terminal Parallel Common"      => "(DPC)",
    "Duel Terminal Rare Parallel Rare"   => "(DRPR)",
    "Duel Terminal Super Parallel Rare"  => "(DSPR)",
    "Duel Terminal Ultra Parallel Rare"  => "(DUPR)",
    "Duel Terminal Secret Parallel Rare" => "(DScPR)",
    "Gold Rare"                          => "(GUR)",
];
echo ($arr[$rarity] ?? 'nothing');

您无需为此编写任何开关或IF-ELSE块。

相关内容

  • 没有找到相关文章

最新更新