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

for task 3 of 3 for foreach loop PHP code challenge- am I going about this correctly?

Am I going about this correctly? I feel like I'm either very close or way off base.

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 $value){
  if ($value['in_stock'] = true) {
  echo $value['name'];
  }
?>

1 Answer

Hey Matthew Lawson,

you're very close! You forgot the extra = sign in your if. So your code should change from this:

if ($value['in_stock'] = true)

to this:

if ($value['in_stock'] == true) {

But you could also do (as an alternative):

if ($value['in_stock']) {

for cleaner code.

The reason why it was still working for you is because you were doing an assignment - you were setting $value['in_stock'] to true. The assignment it self is successful (aka true) so the if gets executed.

AHH!! yay!! thank you! Still learning how to express myself best when it comes to booleans.