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

Dallas Freeman
Dallas Freeman
3,637 Points

"...Did you change it?" ...nope

Challenge says: Using a foreach, create a loop that goes through each of the flavors in the $flavors array.

Here's the code: <?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 $flavors) { echo $flavors["name"] . "<br />\n" ; } ?>

Error I get says: "Hmmm ... the $flavors array does not have the correct values. Did you change it?"

No, I didn't touch the array, and there's no way what I entered actually changed the array, right? This is now the 3RD time in this new PHP course that my code has been functional and done what the challenge wanted, but it has a small issue with my code for which it won't give me a helpful explanation. I've had to post my problems here and use someone else's nearly identical code that does the same thing to pass. If I'm making mistakes, that's fine, but it's not telling me what I did wrong. So I'm stuck.

Kent Åsvang
Kent Åsvang
18,823 Points

Try this :

foreach ($flavors as $flavor) {
    echo $flavor["name"] . "<br />\n"; } ?>
Dallas Freeman
Dallas Freeman
3,637 Points

Thanks Kent, your change from $flavors to $flavor worked. Can you tell me why that works? Or why the challenge doesn't like it the way I had it? We didn't have anything set as $flavor, only $flavors.

1 Answer

Kent Åsvang
Kent Åsvang
18,823 Points

I can try.

When you have an array, say :

$arrayValues = array("value 1", "value 2", "value 3");

With a foreach-loop, you assign every value inside the array to a new variable so you can access and manipulate it. That is why you can´t use two identical variable names inside the parenthesis of the foreach-loop. Because then you just access the array as a list or something useless like that.

foreach ($arrayValues as $value) {
    echo $value; 
};  

When you use the right syntax, the loop goes trough the array from key value 0, adds this value to $value, then echo´s it to the console, then it iterates trough again and assigns the next key-value 1, adds this to the $value-variable and so forth.

it would go something like this:

  1. takes $arrayValues key value 0, which is "value 1", and assigns this to the $value variable. So right now: $value = "value 1";

2 echo´s $value

3 Assign key value 1, which would be "value 2" to $value variable. So no the variable is overwritten - > $value = "value 2";

4 echo´s $value. etcetc.

You might see why there would be a problem with using the same variable names inside the parenthesis

I hope this helped, I am really a novice myself and don´t have such a good grasp on PHP yet.

Here´s a link that might help if you need more documentation: http://php.net/manual/en/control-structures.foreach.php

S Ananda
S Ananda
9,474 Points

Thanks Kent,

Your explanation helped me understand foreach loops better. I'm still a little uncomfortable with them, but at least I feel like there might be light at the end of the tunnel.