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 trialChris Kyle
9,891 PointsProblem with the foreach exercise
I am having a problem with the 3rd part of the foreach loop exercise. When it asks for me to modify the foreach loop to only display the flavors in stock, I am adding a conditional. It then says task 1 is no longer passing, but the code I wrote works in workspaces and previews correctly in workspaces, just not in the challenge.
<?php
$flavors = array();
$flavors[] = array("name" => "Cookie Dough", "in_stock" => true);
$flavors[] = array("name" => "Vanilla", "in_stock" => false);
$flavors[] = array("name" => "Avocado Chocolate", "in_stock" => false);
$flavors[] = array("name" => "Bacon Me Crazy", "in_stock" => true);
$flavors[] = array("name" => "Strawberry", "in_stock" => false);
//add your code below this line
foreach ($flavors as $key => $type) {if ($type["in_stock"] {
echo $type["name"];
}
}
?>
1 Answer
Amanda Allison
9,104 PointsIt looks like there's a closing parenthesis missing at the end of the conditional statement. It should be: if ($type["in_stock"])
foreach ($flavors as $key => $type) {
if ($type["in_stock"]) {
echo $type["name"];
}
}