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

Could Somebody Tell Me What I did Wrong?

Hey guys,

I am struggling to past this challenge. I am not sure what I am not understanding. I have gone back and watched the videos over again & i'm still not getting it. Could someone explain to me where I am going wrong and how to fix it.

Thanks, -Anthony

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 $f) {
   echo $f['Cookie Dough'] . "<br>";
   echo $f['Vanilla'] . "<br>";
   echo $f['Avocado Chocholate'] . "<br>";
   echo $f['Bacon me Crazy'] . "<br>";
   echo $f['Strawberry'] . "<br>";
    if ($f['in_stock'] == true) {
      echo $f['in_stock'] . "\n";
    }
}
?>

1 Answer

Hi !

They didn't ask you to copy each of the flavors by hand, of course! :) But rather to do it thanks to the foreach loop, this way :

foreach ($flavors as $f) {
  if ($flavors->in_stock) {
    echo $f->name;
  }
}

Oh right, okay. Thanks for the help. Feel a bit silly now but thanks again xD