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 Basic PHP Website (2018) Listing and Sorting Inventory Items Array Keys

Karn Sylvester
Karn Sylvester
11,179 Points

Temporary Keys assigned and displaying but unable to continue

Hi everyone,

I believe I have completed the first half of this task and when I preview my working out it does show the ISBN alongside the book title. However when it is submitted it displays an error.

Can anyone see where I am going wrong?

index.php
<?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 => $isbn) { ?>
            <li><?php echo $book . " " . $isbn; ?></li>
        <?php } ?>
    </ul>
</body>
</html>

1 Answer

jamesjones21
jamesjones21
9,260 Points

the challenge requires the isbn to be the key and name of book to be the value.

So you will need to change your foreach look so the key variable = $isbn and value to be $book

like so:

<?php foreach($books as $isbn => $book) { ?>
            <li><?php echo $isbn . ' ' . $book; ?></li>
 <?php } ?>

output will look like:

978-0743261690 Gilgamesh

978-0060931957 The Odyssey

978-0192840509 Aesop's Fables

978-0520227040 Mahabharta

978-0393320978 Beowulf

You were almost there to , but with these challenges, they expect the variables to be named and in the correct spots. Even though your foreach would work the same way with book as key and isbn for value.

Karn Sylvester
Karn Sylvester
11,179 Points

Thanks a lot for your answer jamesjones21; I'll keep an eye out for the correct order in the future. Thanks again!