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
Elissa Collier
8,530 PointsNeed hep with "Array Keys" Code Challenge
Question:
The array below contains a list of books. Each element in the array has the book’s title as its value and the ISBN as its key. Right now, the page is only displaying the book titles in the browser. In this code challenge, we will modify the page to also display each book’s ISBN. First, we need to make the keys from the books array accessible inside the foreach loop. Modify the foreach command so that, as it loops through the books, it loads the ISBN for each book into a working variable called $isbn.
Here's the code it starts with:
<?php
$books["978-0743261690"] = "Gilgamesh";
$books["978-0060931957"] = "The Odyssey";
$books["978-0192840509"] = "Aesop's Fables";
$books["978-0520227040"] = "Mahabharta";
$books["978-0393320978"] = "Beowulf";
?><html>
<head>
<title>Five Great Books</title>
</head>
<body>
<h1>Five Great Books</h1>
<ul>
<?php foreach($books as $book) { ?>
<li><?php echo $book; ?></li>
<?php } ?>
</ul>
</body>
</html>
I have to idea how to make the keys from the books array accessible inside the foreach loop and how to modify the foreach command so that, as it loops through the books, it loads the ISBN for each book into a working variable called $isbn.
The only clue I get is "you should be using a double arrow". But where exactly do I have to use that? Inside the foreach loop? Doesn't seem to work..
Please help :)
9 Answers
Randy Hoyt
Treehouse Guest TeacherHere's a link to the video where I discuss this syntax:
At 4:10 in that video, you can see the syntax you'll need.
Does that help?
Kevin Korte
28,149 PointsI don't want to give you too much help right away...I'll let you struggle a bit. But I'll give you more and more hints until you solve it. :)
First,
There are two moving parts. Loading the ISBN into the foreach loop as a working variable, and second printing the ISBN value to the screen in the echo statement by concatenating the isbn variable with the book variable.
For the foreach loop. The "key" is the value inside the square brackets. In this case it is the ISBN.
So to load that into the foreach loop, it tells you to use the => sign.
So you might have something like:
$book => $isbn
See if you can figure out the rest.
Elissa Collier
8,530 PointsOkay, thanks Randy, that worked! Now stuck on the next one, haha.
"Next, modify the code inside the foreach loop to display the $isbn in parentheses. After the title, add a space, an opening parenthesis, the ISBN, and a closing parenthesis."
This is what I got:
<ul>
<?php foreach($books as $isbn => $book) { ?>
<li><?php echo $book . "\n" . "($isbn)" ; ?></li>
<?php } ?>
</ul>
I get the message "I see all the book titles and the ISBNs in the browser, but the formatting is not quite right. Each list item should look like this: Book Title (978-0123456789)", but in the preview it looks right actually...
Randy Hoyt
Treehouse Guest TeacherThe code \n creates a line break, not a space. I think changing this ...
. "\n" .
... to this ...
. " " .
get everything working correctly for you.
Antonio English
Front End Web Development Techdegree Graduate 18,103 Pointsthis is what I did.
<?php
$books["978-0743261690"] = "Gilgamesh"; $books["978-0060931957"] = "The Odyssey"; $books["978-0192840509"] = "Aesop's Fables"; $books["978-0520227040"] = "Mahabharta"; $books["978-0393320978"] = "Beowulf";
?><html> <head> <title>Five Great Books</title> </head> <body> <h1>Five Great Books</h1> <ul> <?php foreach($books as $isbn => $book ) { echo "<li>"; echo $book; echo "</li>"; echo "<li>"; echo $isbn; echo "</li></br>"; } ?> </ul> </body> </html>
Roberto Guajardo
Courses Plus Student 7,014 PointsThe array below contains a list of books. Each element in the array has the book's ISBN as its key and the book's title as its value. Right now, the page is only displaying the book titles in the browser. In this code challenge, we will modify the page to also display each book's ISBN.
First, we need to make the keys from the $books array accessible inside the foreach loop. Modify the foreach command so that, as it loops through the books, it loads the ISBN for each book into a working variable called $isbn.
NOTE: "as $isbn" creates the variable which is the "ISBN as its key": "$books["isbn"]" (from the array).
At the same time, it should continue loading the title of each book into the $book variable.
NOTE: "=> $book" is the title of your book: " = "title" (from the array) .
<?php foreach($books as $isbn => $book) { ?>
<li><?php echo $isbn ." ". $book; ?></li>
<?php } ?>
I hope this explains it. I tested and it works.
Cassandra James
7,561 PointsI have been trying and tweaking for hours now with no success.
Here is my code for line 17 <li><?php echo $book . " " ."($isbn)"; ?>
this is the error that I get Bummer! I see all the book titles and the ISBNs in the browser, but the formatting is not quite right. Each list item should look like this: Book Title (978-0123456789)
I have tried several variations of the code above trying to add the space and opening parenthesis.
Please help :)
Kevin Korte
28,149 PointsYou quotes are incorrect. It should be something more like
<?php echo $book . " " ."(" . $isbn . ")"; ?>
Elissa Collier
8,530 PointsThank you so much Randy! :)
Harvey Clear
3,025 PointsHi Randy,
Thanks for the help. What i don't get, is how does the server know what $isbn relates to?
Kevin Korte
28,149 PointsThe server knows what $isbn relates to from the foreach loop. We load each part of the array into two working variables, one of which is $isbn
MUZ140286 Ronald Mutsikwi
14,788 PointsBummer! The HTML for this page is not quite right: <html><head><title>Five Great Books</title> (' . $isbn . ') </head><body><h1>Five Great Books</h1><ul><li>Gilgamesh (978-0743261690)</li><li>The Odyssey (978-0060931957)</li><li>Aesop's Fables (978-0192840509)</li><li>Mahabharta (978-0520227040)</li><li>Beowulf (978-0393320978)</li></ul></body></html>
Daniel Cudney
Courses Plus Student 1,378 PointsDaniel Cudney
Courses Plus Student 1,378 Pointsthank you for linking this video because it helped me understand it. More perspectives the better! :D
Mikko Metso
22,375 PointsMikko Metso
22,375 PointsBroken link after March 31, 2016. ("Build a Simple PHP Application" was retired on March 31, 2016. You are now viewing the recommended replacement.)