Как я могу упростить этот код, чтобы в нем не было так много операторов if if?

Я уверен, что это можно упростить, но я не совсем уверен, как. Любая помощь будет принята с благодарностью!

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

0

Решение

Похоже, ваш $k это JSON-кодированный массив, поэтому:

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

Другие решения

Других решений пока нет …

По вопросам рекламы [email protected]