$score = $score + $ingredientData["score"]; } else { // not enough ingredients for recipe, set score to -1 // mark as invalid $score = -1; break; } } else { $score = -1; } } $recipes->setScore($key, $score); } // we now have a score value for each recipe based on ingredients expiry // order the recipes by order $recipes->sort(); // filter out all invalid recipes $results = $recipes->filter(); // check to make sure there is a recipe //the top result is the best recipe based on the ingredients expiry if (count($results)) { $result = array_shift($results); $message->add("The best recipe for you to make is : " . $result["name"])->add("You need the following ingredients:"); foreach ($result["ingredients"] as $ingredient) { $message->add(" - " . $ingredient["amount"] . " " . $ingredient["unit"] . " of " . $ingredient["item"]); } // add a blank line $message->add(""); } else { // order takeout $message->add("All ingredients are past their used by date, Order Takeout!");