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 trialPeter Smith
4,305 PointsGetting correct answer, but states "You are not using the variable you defined in the foreach loop."
I don't understand why this question is incorrect. The variable I created in the two foreach loops all seem used.
<?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
$filter = array();
foreach ($flavors as $key => $item) {
if ($item['in_stock'] == true) {
$filter[$key] = $item;
}
}
foreach ($filter as $id => $value) {
echo $flavors[$id]['name'] . '<br />';
}
?>
1 Answer
Steven Ang
41,751 PointsYou overthink it. You don't need a new array to store all of the flavours that are available or creating another foreach loop to display them. You just need a single foreach loop and echo the flavours that are available. This is all you need:
<?php
foreach ($flavors as $flavor) {
if ($flavor["in_stock"] === true) {
echo $flavor;
}
}
?>