Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

PHP PHP Arrays and Control Structures PHP Loops Looping with PHP

Chris Hummel
Chris Hummel
13,960 Points

code is right, but get error

I tested my code outside of this test, and it displays the flavors to the screen, but I still get an error saying I didn't use the same variable. What gives?

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) {
  echo $flavors[$key]["name"] . "<br />\n";
}
?>

1 Answer

Jordan Howlett
Jordan Howlett
3,369 Points

It seems like you may have overthought the solution, take a look at this piece of code and see if it helps clarify what they were asking for.

foreach ($flavors as $flavor) {
  if($flavor["in_stock"] == true){
  echo $flavor['name'];
  }
}