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 trial

PHP PHP Arrays and Control Structures PHP Loops Looping with PHP

Modify the foreach loop to display only the flavors that are in stock.

Modify the foreach loop to display only the flavors that are in stock.

index.php
<?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 $name)
{
echo $name['name'];
}
?>

2 Answers

Julian Gutierrez
Julian Gutierrez
19,201 Points

For this part of the challenge insert your echo statement inside of an if statement that checks if "in_stock" is true.

Vid Majkus
Vid Majkus
10,005 Points

I managed to show only item that are in stock, however it says then tasts 2 is no longer passing. What do I need to do that everything will pass? Thanks for help!

This is my 2 of 2 code:

foreach($flavors as $item) { echo $item['name'] ; }

This is my 3 of 3 code:

$status = true;

$inStock = []; if($status == 'all'){ $inStock = array_keys($flavors); }else { foreach($flavors as $key => $item) { if($item['in_stock'] == $status){ $inStock[] = $key; } } } foreach($inStock as $id){ echo $flavors[$id]['name']; }