Heads up! To view this whole video, sign in with your Courses account or enroll in your free 7-day trial. Sign In Enroll

- 2x 2x
- 1.75x 1.75x
- 1.5x 1.5x
- 1.25x 1.25x
- 1.1x 1.1x
- 1x 1x
- 0.75x 0.75x
- 0.5x 0.5x
We'll use our collection to create a shopping list that combines all recipe ingredients.
NOTE in_array vs array_key_exists
in_array
will check the "value" in an array while array_key_exists
will check the "key" in an array.
Code Sample
This code will NOT handle all plural vs singular ingredients, but this will get you started. You could be adding "es" or changing "y" to "ies".
public function getCombinedIngredients()
{
$ingredients = array();
foreach ($this->recipes as $recipe) {
foreach ($recipe->getIngredients() as $i) {
$item = $i['item'];
if (strpos($item, ",")) {
$item = strstr($item, ",", true);
}
if (array_key_exists($item . "s", $ingredients)) {
$item .= "s";
} else if (array_key_exists(substr($item,0,-1),$ingredients)) {
$item = substr($ingredient,0,-1);
}
$ingredients[$item][] = array($i["amount"],$i["measure"]);
}
}
return $ingredients;
}
Documentation
Challenge
Setup calculations to return the amount of each item.
Related Discussions
Have questions about this video? Start a discussion with the community and Treehouse staff.
Sign up-
Matt Nickolls
9,895 Points2 Answers
-
Matt Nickolls
9,895 Points3 Answers
-
Kacy Weakley
16,209 Points0 Answers
-
Zijun Liao
13,409 Points0 Answers
-
jlampstack
23,932 Points4 Answers
-
György Varga
19,198 Points0 Answers
-
Justin Radcliffe
18,987 Points2 Answers
-
Jeff Styles
2,784 Points0 Answers
-
Alvin Hue
4,496 Points1 Answer
-
MOD
Jonathan Grieve
Treehouse Moderator 91,254 Points0 Answers
-
maze
17,027 Points4 Answers
-
Darrell Conklin
Python Development Techdegree Student 22,377 Points7 Answers
-
Trần Phúc
2,264 Points0 Answers
View all discussions for this video
Related Discussions
Have questions about this video? Start a discussion with the community and Treehouse staff.
Sign up
You need to sign up for Treehouse in order to download course files.
Sign upYou need to sign up for Treehouse in order to set up Workspace
Sign up