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

Perrie Banks
Perrie Banks
6,500 Points

What am I doing wrong?

No matter how many times I add in a variable, it still doesn't seem to register. I don't know if I'm suppose to make up my own variable or take it from the previous answer.

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 $key => $value) {
if ($value ["in_stock"] == true) {
  echo $value["name"]. "<br />";}

}

foreach ($flavors as $item) {
  echo $item['name'] . "<br />\n";}

foreach ($flavors as $value) {
  if ($value["in_stock"] == true) {
  echo $value["name"] . "<br />\n";
  }
}

?>

3 Answers

Antonio De Rose
Antonio De Rose
20,885 Points
<?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 => $value) {
if ($value ["in_stock"] == true) {
  echo $value["name"]. "<br />";}

}

/*foreach ($flavors as $item) {
  echo $item['name'] . "<br />\n";}

foreach ($flavors as $value) {
  if ($value["in_stock"] == true) {
  echo $value["name"] . "<br />\n";
  }
}*/

un-comment the part, as I have mentioned above and try, I think
that is what is causing the issue

?>
Perrie Banks
Perrie Banks
6,500 Points

Which part am I un-commenting? I was trying different ways and it still wasn't working.

Antonio De Rose
Antonio De Rose
20,885 Points

you can, all different ways, in all one go, for that you have to setup your own dev environment, or use an online editor like a phpfiddle, given at this context to answer the question, you cannot try, all options in one go.

Perrie Banks
Perrie Banks
6,500 Points

I think I figured out. Thanks so much!