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 Build a Simple PHP Application Integrating with PayPal Forms, Arrays, and Get Variables

foreach

Hi,

good day,

I don't really get this. why is the answer 01? and I can't still get the answer to the other question that echos out $b.

<?php

$flavors = array("Cake Batter","Cookie Dough");

foreach ($flavors as $a => $b) {

echo $a;

}

?>

2 Answers

Jeremy Germenis
Jeremy Germenis
29,854 Points

The answer is 1 because the $a represents the key number of the item and php starts counting at 0.

[0] => "Cake Batter" [1] => "Cookie Dough"

Well actually in the foreach loop you are telling it to print the key for each element in the array. Sense the $a variable represents the key, it only prints that up for each loop. Try rewriting this code so that the echo statement prints the key and value for each, and see if that makes sense. replace echo $a with echo $a . " " . $b . "\n"; See if that makes more sense to you.