How can I optimize this code? [closed]

You’re assigning a value to $dish, but then not using it…

Try the following. It takes advantage of the fact that your image for “gluten” is named gluten.png, etc. You’ll be able to add more tags by just adding to the $images array.

$images = array(
    "gluten"     => "Gluten Free",
    "vegan"      => "Vegan Friendly",
    "vegetarian" => "Vegetarian"
);
$dish_tags = (array) get_field('diet_friendly');

foreach ($images as $image_name => $alt) {
    if (in_array($image_name, $dish_tags)) {
        echo '<img src="' . get_template_directory_uri() . '/images/' .
            $image_name . '.png" alt="' . $alt . '" />';
    }
}