Heads up! To view this whole video, sign in with your Courses account or enroll in your free 7-day trial. Sign In Enroll
Start a free Courses trial
to watch this video
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.
The cookbook is coming along nicely, and 0:00 we've had the chance to explore filtering and passing objects between collections. 0:02 By using an object, we're able to group the data and 0:07 the functionality of our cookbook into one place. 0:10 There's one more feature that I'd really like to have for this collection. 0:13 A shopping list that combines all recipe ingredients. 0:17 Like we did with listing the recipe titles, we pull the data in our recipe 0:21 collection, then use our render class to display the data. 0:25 We'll start out with a simple list of items that you 0:29 can expand to show a more detailed representation. 0:33 In our recipes collection, let's add a new method, 0:37 public function getCombinedIngredients(). 0:41 We want to create a master list of ingredients, 0:52 so let's initialize an ingredients array. 0:55 We're going to want to loop through each recipe in this collection, 1:04 then loop through all the ingredients in each of those recipes. 1:07 We'll add the item as the key in our array. 1:30 Then we'll add an inner array element to that ingredient. 1:40 We'll give it the amount, And the measure. 1:48 Now we can return the ingredients. 2:00 We now have our master array of ingredients from our collection of 2:06 recipes. 2:10 Next, we need to set up the display in the render class. 2:10 We'll create a new method. 2:16 This is a static function that we'll call listShopping. 2:22 And we'll pass in the ingredient_list. 2:29 The first thing we want to do is sort on the key, so 2:38 we use ksort with our ingredient list. 2:42 Now we're ready to return our shopping list. 2:51 We'll use the implode function again. 2:53 Have you noticed how much I like this function? 2:56 This time, we want to return the keys instead of the values, so 3:05 we're going to use another built-in function called array_keys. 3:09 And we pass it our array. 3:16 Let's go back to the cookbook file and 3:22 use our shopping list on our breakfast collection. 3:25 We render our list, shopping, and we pass it, our breakfast, getCombinedIngredients. 3:35 Let's add a title to this. 3:42 Now we're ready to run the script. 3:54 We see some of our ingredients, but they don't all fit in this console, so 4:03 let's display this in a browser. 4:07 We're going to View Page Source, now we can see a better list. 4:13 I see a nice shopping list of items sorted alphabetically. 4:18 The problem is that some of our ingredients are duplicated. 4:22 We have Eggs, singular, Eggs, plural, and Eggs, Beaten. 4:26 Let's modify our getCombinedIngredients 4:32 method to better combine these ingredients. 4:36 We're going to be doing a few things with the items, so let's create a new variable. 4:39 We'll call this item, we'll set it equal to the ing["item"]. 4:44 Now let's also change our variable within our ingredients to item. 4:52 The first thing I want to do is check if our item contains a comma. 4:57 If so, we want to use only the part before the comma. 5:02 I'm going to be using a couple more built-in PHP functions. 5:07 So check the teacher's notes to learn more about each of these functions. 5:10 if (strpos, for string position, and we're gonna check our item, 5:15 and we're going to check for the string position of a comma. 5:21 This will return the first numeric position of the first 5:26 occurrence of our search, which is the comma. 5:32 If our comma is not found, it will return false. 5:36 I also want to note that the position is zero-indexed. 5:40 So if the comma is in the first position, it will return 0, 5:44 which would also evaluate to false. 5:48 That's perfect for our use case here, because if a comma is the first character, 5:50 there's nothing to grab before the comma. 5:55 If the comma is found in any other position, 5:58 this statement will evaluate to true. 6:01 And we can grab only the part of the string before the comma. 6:03 I'm going to set item = strstr to pull a portion of the string. 6:08 I'll pass the item string, the comma for the search. 6:15 And finally, true, 6:21 which will tell the function to return the portion before the comma. 6:22 Let's save this and refresh the browser. 6:27 Now we can see that we no longer have our Eggs, Beaten. 6:33 But we still have both the singular and plural form of eggs. 6:37 So next, I want to check if this item is already in the array 6:41 as its singular or plural version. 6:46 I'm going to start by checking if our item ends in an S. 6:49 I'll use if (substr, start with the item, and 6:53 then we're going to use -1 to grab the last character in this string. 6:59 I then compare this last character to the character s. 7:06 If the last character is an s, I also want to check if 7:12 the item exists as a key in the array without an s. 7:17 So we'll combine our comparisons, and 7:21 then we'll use the function array_key_exists. 7:25 Next, we'll use the rtrim function to 7:30 trim the s from the right side of our item string. 7:34 And we're going to check in the ingredients array. 7:39 If a key exists without an s, we'll remove the s from our item. 7:49 So item = rtrim, 7:55 the item s. 7:59 Finally, if the item did not end with an s, 8:03 we're going to check if the item with an s is a key in the array. 8:07 else if (array_key_exists, and 8:13 we're going to pass the item, plus an s. 8:18 And check our ingredients If so, 8:27 we want to use the item with an s, item += "s". 8:34 Let's go back to the browser one last time. 8:39 This time, we'll see that we only have 8:42 a single item that starts with Egg. 8:47
You need to sign up for Treehouse in order to download course files.
Sign up