Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

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

Abdullah Howard
Abdullah Howard
11,914 Points

PHP Basic looping over an object

Hey guys. This code challenge has me stumped. It is telling me to assign the keys of the object to a temporary variable named $isbn and I have done that. When I log it out to the page it loads fine. But for some reason the code editor is telling me that it is wrong. Can anyone help me out?

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

1 Answer

Jordan Howlett
Jordan Howlett
3,369 Points

Hi! Take a look at what the question is asking you here. You're using $key as a working variable, while what you're doing is working, it is not what the question is specifically asking for. Substitute your $key for the $isbn variable in your foreach loop, you can also delete the extra $key variable declaration you have in there too.

<?php
  $books = array(
    "978-0743261690" => "Gilgamesh",
    "978-0060931957" => "The Odyssey",
    "978-0192840509" => "Aesop's Fables",
    "978-0520227040" => "Mahabharta",
    "978-0393320978" => "Beowulf",
  );
?>
<html>
<head>
    <title>Five Great Books</title>
</head>
<body>
    <h1>Five Great Books</h1>
    <ul>
        <?php 
      //Display the key (isbn) as well as the value (title)
        foreach($books as $isbn => $book) {
            echo"<li>" . $isbn . $book . "</li>";
         } 
        ?>
    </ul>
</body>
</html>
Abdullah Howard
Abdullah Howard
11,914 Points

Ty I'll pay more attention next time.