Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trialMatt Nickolls
9,895 PointsConfused about the getCombinedIngredients function
public function getCombinedIngredients()
{
$ingredients = array();
foreach ($this->recipes as $recipe) {
foreach ($recipe->getIngredients() as $ing) {
$item = $ing["item"];
if (strpos($item, ",")) {
$item = strstr($item, ",", true);
}
if (substr($item, -1) == "s" && array_key_exists(rtrim($item, "s"), $ingredients)) {
$item = rtrim($item, "s");
} else if (array_key_exists($item. "s", $ingredients)) {
$item .="s";
}
$ingredients[$ing["item"]] = array(
$ing["amount"],
$ing["measure"]
);
}
}
return $ingredients;
OK, so the above code is, I think, the exact code for the getCombinedingredients function demonstrated in the video. I have typed it out manually following along from the video. I checked and checked it again. But I still get both the'Eggs' and 'Eggs, Beaten' items displayed to the browser, whereas Alena only gets a singular 'Egg' item.
This is very confusing. I thought maybe the $ingredients array needed to be unset because it was being modified by each successive execution of code, but that doesn't seem to be the case.
In my result it would appear that the first if statement, that is supposed to only return the portion of a string before a comma, is not working. In Alena's video all the items that have string content separated by a comma have been stripped to just the portion before the comma. In my code none of them have. So the problem seems to be there. Any help Welcome.
What makes it more difficult to figure out is that the code in the downloadable files has different logic to that demonstrated in the video and goes as follows:
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 (in_array($item . "s", $ingredients)) {
$item .= "s";
} else if (in_array(substr($item,0,-1),$ingredients)) {
$item = substr($ingredient,0,-1);
}
$ingredients[$item][] = array($i["amount"],$i["measure"]);
}
}
return $ingredients;
}
using this logic returns one plural 'Eggs'. Rather than just a singlar 'Egg' as per the video. Whilst it is useful to see an alternative way of doing it, it doesn't help at all with comprehending the logic. For the files to be valuable as a teaching aid they need to be consistent with the video surely?
It is a little bit disappointing to see that a number of other students have had the same difficulty with this same bit of code over the last few years and no representative from Treehouse has provided and answer or assisted. Students are also commenting that the download files don't match the video, some as long as three years ago. And still it hasn't been revised? It's really good to have challenging teaching material like this, but it is very frustrating to feel you have to move on from a video without feeling you have entirely understood the logic. Please can someone kind from Treehouse help here.
2 Answers
Jennifer Nordell
Treehouse TeacherHi there, Matt Nickolls! Yes, I believe your logic is spot on. I can't really answer on what happened here, exactly. I feel like maybe Alena was showing the results of a different set of recipes that were given the "breakfast" tag or something. However, if you go to the next to last video and grab the project files from the "Downloads" section, you will find that running cookbook.php
also gives a single "Eggs" exactly like you get.
I wish I could be of more assistance, but I will put this before the education team to see what me might be able to do about this
Hope this helps!
Matt Nickolls
9,895 PointsThanks Jennifer that is lovely. Sorry to sound like a Grumps! :)