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

Array Key

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. How do you want this. I maybe wrong, but I may have to go back. I'm a little confused. Cabn you give me a hint?

7 Answers

Randy Hoyt
STAFF
Randy Hoyt
Treehouse Guest Teacher

The line that says this ...

foreach($books as $book) {

... needs to be modified. Right now, it loads the book title into the $book variable but it doesn't do anything with the ISBN. It needs to be modified so that it continues to load the book title into the $book variable but all loads the ISBN into the $isbn variable.

It doesn't look like you are loading the ISBN into a working variable. (Here's a clue: you should be using a double arrow.)

Where does the double arrow go? foreach ($books as $book) { }

as it loops through the books, it loads the ISBN for each book into a working variable called $isbn. "What?"

Randy Hoyt
STAFF
Randy Hoyt
Treehouse Guest Teacher

Change it to this:

foreach($books as $isbn => $book) {

Here is another problem by me for you? foreach ($books as $isbn => $books ) {

}

Sorry it say's to try again?

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). Here is my code: Five Great Books Five Great Books <?php foreach($books as $isbn => $book) { ?> <?php echo $book; ?> (<?php echo $isbn; ?>) <?php } ?>

Randy Hoyt
STAFF
Randy Hoyt
Treehouse Guest Teacher

It looks like you might have removed some of the HTML that was there before. The goal of this task is to add some information from the array to the page, while leaving the existing HTML that was there in tact. When the page first loaded, it displayed HTML like this:

<html>
<head>
    <title>Five Great Books</title>
</head>
<body>
    <h1>Five Great Books</h1>
    <ul>
                    <li>Gilgamesh</li>
                    <li>The Odyssey</li>
                    <li>Aesop's Fables</li>
                    <li>Mahabharta</li>
                    <li>Beowulf</li>
            </ul>
</body>
</html>

It seems like you might have removed the unordered list and list item tags.

Does that help?