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

Sahou Kanaan Almelhem
PLUS
Sahou Kanaan Almelhem
Courses Plus Student 15,547 Points

foreach with associative array problem ??

<?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 => $item) { echo "<p>" . $item[$key]['name'] . "</p>"; }

/*the output is Notice: Undefined offset: 0 in /home/treehouse/workspace/flavors.php on line 12

Notice: Undefined offset: 1 in /home/treehouse/workspace/flavors.php on line 12

Notice: Undefined offset: 2 in /home/treehouse/workspace/flavors.php on line 12

Notice: Undefined offset: 3 in /home/treehouse/workspace/flavors.php on line 12

Notice: Undefined offset: 4 in /home/treehouse/workspace/flavors.php on line 12*/ ?>

2 Answers

Robert Kulagowski
Robert Kulagowski
4,954 Points

Your code actually contains an array of arrays. I've added a var_dump($flavors) before the foreach.

array(5) {
  [0] =>
  array(2) {
    'name' =>
    string(12) "Cookie Dough"
    'in_stock' =>
    bool(true)
  }
  [1] =>
  array(2) {
    'name' =>
    string(7) "Vanilla"
    'in_stock' =>
    bool(false)
  }
  [2] =>
  array(2) {
    'name' =>
    string(17) "Avocado Chocolate"
    'in_stock' =>
    bool(false)
  }
  [3] =>
  array(2) {
    'name' =>
    string(14) "Bacon Me Crazy"
    'in_stock' =>
    bool(true)
  }
  [4] =>
  array(2) {
    'name' =>
    string(10) "Strawberry"
    'in_stock' =>
    bool(false)
  }
}

If you look at the foreach, it's iterating over the outside of the array, so it's looking at [0] through [4] as the $key, and $item is an associative array with "name" and "in_stock" as the indexes. But in the echo, you're telling the echo to print out the $key (which is only valid in the outer array) of the $item, and it doesn't exist there.

If $key is important, then you'd change your echo to something like this:

echo "<p>key:$key " . $item['name'] . "</p>";

Which will output:

<p>key:0 Cookie Dough</p><p>key:1 Vanilla</p><p>key:2 Avocado Chocolate</p><p>key:3 Bacon Me Crazy</p><p>key:4 Strawberry</p>

Does that make sense?

Sahou Kanaan Almelhem
PLUS
Sahou Kanaan Almelhem
Courses Plus Student 15,547 Points

thank you very much :) i have understood what exactly $item and $key mean.