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

Cliff Jackson
Cliff Jackson
2,887 Points

Cannot get foreach loop to output???

I think of the video ahead of this challenge was not as complicated and long i would get this but no matter what i do it won't work. It keeps saying 'name' element??

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 "$key . $value";
}
?>

1 Answer

Steven Parker
Steven Parker
229,608 Points

Since each element of the outer array is itself an associative array, you need to use indexing to get to the name. Also, you only need the name of the value (not the key):

  echo "$value[name]";