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 keys PHP

i found my self terrible at doing web programming, but im trying my very best to keep up with PHP course. at the moment im stuck at Code Challenge: Array Keys, at stage 1.

Here's the question: 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.

Provided code:

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

here is what i added:

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

and i even tried doing this

$books = array (

"Gilgamesh" => 978-0743261690,
"The Odyssey" => 978-0060931957,
"Aesop's Fables" => 978-0192840509,
"Mahabharta" => 978-0520227040,
"Beowulf" => 978-0393320978
);

honestly im strugelling learning programming. its best if you can provide information that i could learn and understand how this works, or direct me to which part of the video i missed.

5 Answers

Randy Hoyt
STAFF
Randy Hoyt
Treehouse Guest Teacher

Very close. What you wanted to do was add it like this:

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

The variable before the double arrow [=>] will get the key of the array element, and the variable after the double arrow will get the value. In this array, the ISBN is the key and the title is the value.

hi, I have a question about this method – is it always the case that the variable before the double arrow gets the key and the variable after gets the value? What about arrays with multiple values – which value gets returned?

I've rewatched the video containing this example, which I understand, but when it comes to the challenges at the end of the section it's these get variable questions that always catch me out.

Hi abdul, what you have done is correct you just need to add the $isbn after the title like this:

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

Does that help?

Please ignore Randy beat me!

omg, how can i missed that! thanks guys!

Randy Hoyt
STAFF
Randy Hoyt
Treehouse Guest Teacher

@Gareth, Thanks for answering; I'll give you a few minutes next time before I answer. :~) Hope you enjoyed the course. I'm writing the next PHP course now.

Randy Hoyt
STAFF
Randy Hoyt
Treehouse Guest Teacher

@abdul, Awesome. Glad you got it and finished the badge. It seems like you're making great progress through the course!