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

Edward Lee
Edward Lee
1,805 Points

Can't figure out Step 2

Can someone explain how they solved this? The last two video were confusing to me.

1 Answer

Kieran Barker
Kieran Barker
15,028 Points

Step 2 asks you to echo out each flavor, so you'd do this:

foreach ($flavors as $flavor) {
  echo $flavor;
}

And for step 3, you need to only echo out the flavors that are in stock, which you need to check with a conditional statement such as a ternary:

foreach ($flavors as $flavor) {
  echo $flavor["in_stock"] ? $flavor : "";
}

Or a regular if statement:

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

Notice that we don't need to check if ($flavor["in_stock"] === true) because the value is already a boolean, hence why we can just do if ($flavor["in_stock"]).

I hope this helps! :-)

Edward Lee
Edward Lee
1,805 Points

Thanks for the thorough explanation! It helped a lot. However, to display only the "name" key of each $flavor, I found that

echo $flavor["name"];

allowed me to do so.