12, "green" => 13, "blue" => 14, ); $colors = array(); preg_match_all("/(\d+) (red|green|blue)/", $h, $colors); foreach (array_combine($colors[2], $colors[1]) as $key => $value) { $bag[$key] = $bag[$key] - $value; if($bag[$key] < 0){$valid = false;} } } if($valid){$sum = $sum + $game_id; echo $game_id . "\n";} } fclose($fd); echo $sum;