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 trialAbdullah Howard
11,955 PointsPHP 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?
<?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
4,377 PointsHi! 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
11,955 PointsAbdullah Howard
11,955 PointsTy I'll pay more attention next time.