如何简化此代码,以便它没有那么多if else if语句?

时间:2015-01-01 01:49:12

标签: php laravel collections eloquent

我很确定这可以简化,但我不确定如何。任何帮助将不胜感激!

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); }
        }
    }

1 个答案:

答案 0 :(得分:9)

看起来你的$k是一个JSON编码的数组,所以:

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