简体   繁体   English

我如何简化此代码,使其没有那么多if语句?

[英]How can I simplify this code so it doesn't have so many if else if statements?

I'm pretty sure this can be simplified but I'm not exactly sure how. 我很确定这可以简化,但是我不确定如何做到。 Any help would be greatly appreciated! 任何帮助将不胜感激!

foreach($color_count as $k => $v) {
        $index = count(json_decode($k, true));
        if($v < $max_color_count[$index]) {
            $diff = $max_color_count[$index] - $v;

            if($k == '["White","Blue","Black","Red","Green"]') { $this->add($filteredCards['White-Blue-Black-Red-Green'], $diff); }

            else if($k == '["White","Blue","Black","Red"]') { $this->add($filteredCards['White-Blue-Black-Red'], $diff); }
            else if($k == '["White","Blue","Black","Green"]') { $this->add($filteredCards['White-Blue-Black-Green'], $diff); }
            else if($k == '["White","Blue","Red","Green"]') { $this->add($filteredCards['White-Blue-Red-Green'], $diff); }
            else if($k == '["White","Black","Red","Green"]') { $this->add($filteredCards['White-Black-Red-Green'], $diff); }
            else if($k == '["Blue","Black","Red","Green"]') { $this->add($filteredCards['Blue-Black-Red-Green'], $diff); }

            else if($k == '["White","Blue","Black"]') { $this->add($filteredCards['White-Blue-Black'], $diff); }
            else if($k == '["White","Blue","Red"]') { $this->add($filteredCards['White-Blue-Red'], $diff); }
            else if($k == '["White","Blue","Green"]') { $this->add($filteredCards['White-Blue-Green'], $diff); }
            else if($k == '["White","Black","Red"]') { $this->add($filteredCards['White-Black-Red'], $diff); }
            else if($k == '["White","Black","Green"]') { $this->add($filteredCards['White-Black-Green'], $diff); }
            else if($k == '["White","Red","Green"]') { $this->add($filteredCards['White-Red-Green'], $diff); }
            else if($k == '["Blue","Black","Red"]') { $this->add($filteredCards['Blue-Black-Red'], $diff); }
            else if($k == '["Blue","Black","Green"]') { $this->add($filteredCards['Blue-Black-Green'], $diff); }
            else if($k == '["Blue","Red","Green"]') { $this->add($filteredCards['Blue-Red-Green'], $diff); }
            else if($k == '["Black","Red","Green"]') { $this->add($filteredCards['Black-Red-Green'], $diff); }

            else if($k == '["White","Blue"]') { $this->add($filteredCards['White-Blue'], $diff); }
            else if($k == '["White","Black"]') { $this->add($filteredCards['White-Black'], $diff); }
            else if($k == '["White","Red"]') { $this->add($filteredCards['White-Red'], $diff); }
            else if($k == '["White","Green"]') { $this->add($filteredCards['White-Green'], $diff); }
            else if($k == '["Blue","Black"]') { $this->add($filteredCards['Blue-Black'], $diff); }
            else if($k == '["Blue","Red"]') { $this->add($filteredCards['Blue-Red'], $diff); }
            else if($k == '["Blue","Green"]') { $this->add($filteredCards['Blue-Green'], $diff); }
            else if($k == '["Black","Red"]') { $this->add($filteredCards['Black-Red'], $diff); }
            else if($k == '["Black","Green"]') { $this->add($filteredCards['Black-Green'], $diff); }
            else if($k == '["Red","Green"]') { $this->add($filteredCards['Red-Green'], $diff); }

            else if($k == '["White"]') { $this->add($filteredCards['White'], $diff); }
            else if($k == '["Blue"]') { $this->add($filteredCards['Blue'], $diff); }
            else if($k == '["Black"]') { $this->add($filteredCards['Black'], $diff); }
            else if($k == '["Red"]') { $this->add($filteredCards['Red'], $diff); }
            else if($k == '["Green"]') { $this->add($filteredCards['Green'], $diff); }
        }
    }

It looks like your $k is a JSON encoded array so: 看来您的$k是JSON编码的数组,因此:

$array = json_decode($k);
$newKey = implode('-', $array);
$this->add($filteredCards[$newKey], $diff);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我添加了以下条件,但不匹配,这是什么原因? 它直接在其他条件下 - I have add below condition but doesn't match , so what is the reason ? it is directly going else condition 我怎样才能更改我的代码,以便我不必为所有单个记录使用脚本? - How can I change my code so that I don't have to use a script for all single record? 如何简化这段代码? (太多的话,否则) - How to simplify this code? (too many if else statment) 如何使wordpress中的PHP if语句没有换行符? - How do I make it so that the PHP if statements in wordpress don't have line breaks? 我如何设置它,这样就不必上载表格中的文件即可将其余的表格数据插入mysql数据库? - How can I set it so a file in a form doesn't have to be uploaded to insert the rest of the form data into mysql database? 如何简化此 PHP 代码? switch() 函数太多 - How I can simplify this PHP code? too many switch() function 如何合并或简化多个语句 - How can I combine or simplify multiple statements 如何使此代码更高效,而不要使用那么多的elseif语句? - How to make this code more efficient instead of having so many elseif statements? 以更简洁的方式重写此代码,无需太多其他代码 - Rewrite this code in a neater fashion, without so many else's PHP-如何重构代码,这样我就不必为每个表单实例复制和粘贴代码了? - PHP - How to refactor code so that I don't have to copy and paste it for each form instance?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM