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 trialJordan Howlett
4,377 PointsShopping List Method
I have watched this video multiple times and looked over my code. I cannot seem to find the problem, I do not get the same output as the instructor. My code is copied verbatim from the video. Any help would be appreciated.
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;
}
1 Answer
Jordan Howlett
4,377 PointsOkay, this is for anyone who may answer. It was a reference error on my part. The $ingrendients[$ing["item"]
should have been written as $ingredients[$item]