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 trialTianni Myers
10,453 PointsForeach Looping Nightmare
Seriously why doesnโt this code work? It says task 1 is no longer completed
<?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 $value) {
if ($value['in_stock'] == true) {
echo $value['name'];
}
?>
2 Answers
Ezra Siton
12,644 Pointssyntax error. You forgot to close the forEach loop (missing "}")
outline:
forEach....{
if{
....
}
}
Also if something feel like "nightmare" use compiler to check your code: https://repl.it/languages/php
Fahri Can
9,921 PointsIf you don't want to use {} you can write your IF-Statement without, for instance:
foreach ($flavors as $value) {
if ($value['in_stock'] == true)
echo $value['name'];
}