PHP Build a Basic PHP Website (2018) Listing and Sorting Inventory Items Array Keys

Karn Sylvester
Karn Sylvester
11,068 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,242 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,068 Points

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