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

help please !!

help!

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 => $item){
      if($key == 'name'){
        echo $key ." ". $item."\n" ;
      }
    }
?>

2 Answers

Antonio De Rose
Antonio De Rose
20,885 Points
<?
//First, the task for the above, does not have an array, but array of arrays, so when you go through the loop, for the first //time, from what have you put, your variable $key would hold the value of 0, as there is no specific, key assigned.

/*Considering this line below
$flavors[''] = array("name" => "Cookie Dough",  "in_stock" => true);*/

/*when the array runs for the first time
Considering your code, $key would hold 0*/

//$item, would hold an array("name" => "Cookie Dough", "in_stock" => true)

/*Had, the array looked like, below
$flavors['a'] = array("name" => "Cookie Dough", "in_stock" => true);*/

//The $key, would have ‘a’

//Going through your code, see my points
foreach($flavors as $key => $item){//$key would have 0, and the $item will hold an array
      if($key == 'name'){//this will be never met, cause the keys, are 0,1,2,3,4
        echo $key ." ". $item."\n" ;//this will throw an error, string combined with an array
      }print_r($key);echo"</br>";//if you have a local setup, run this line, you will get a broader picture.
    }
//if you do not have a local setup, run the below code in, http://phpfiddle.org/, paste your code in codespace tab, hit run, you would see the output
$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);

foreach($flavors as $key => $item){//$key would have 0, and the $item will hold an array
      if($key == 'name'){//this will be never met, cause the keys, are 0,1,2,3,4
       // echo $key ." ". $item."\n" ;//this will throw an error, string combined with an array
      }echo($key);echo "=====";print_r($item);echo"<br>";//if you have a local setup, run this line, you will get a broader picture.
    }
?>

thanks everyone it worked with me !!! :D