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 Creating the Display Function

Alexander Bogdanov
Alexander Bogdanov
2,587 Points

Why do we need the $id variable??????

ok so look at the code below:

This:

function get_item_html($item) { $output = "<li><a href='#' /><img src='" . $item["img"] . "' alt='" . $item["title"] . "' />" . "<p>View Details</p>" . "</a></li>"; return $output; }

            foreach($catalog as $item) {
                echo get_item_html($item);
            }

looks absolutely the same as this:

function get_item_html($id, $item) { $output = "<li><a href='#' /><img src='" . $item["img"] . "' alt='" . $item["title"] . "' />" . "<p>View Details</p>" . "</a></li>"; return $output; }

            foreach($catalog as $id => $item) {
                echo get_item_html($id, $item);
            }

My question is why do you need to declare the variable $id and what does this do: foreach($catalog as $id => $item) {

}

I mean I don't understand the part $id => $item thanks!

3 Answers

Simon Coates
Simon Coates
28,694 Points
foreach($catalog as $id => $item) {

this iterates across the $catalog array. For each iteration, it assigns the associative array key to $id, and the value to $item. You use this syntax when the key matters. The following shows some examples:

<?php
$list = ['red', 'white', 'blue'];
foreach($list as $listItem){
    echo $listItem . " ";
}
echo "\n";
foreach($list as $key=>$listItem){
    echo "key:".$key." value:".$listItem . " ";
}
echo "\n";

//More Natural associative array
$nameToEyeColor = [
    "beth"=>"brown",
    "arnold"=>"generally bloodshot",
    34 => "this is a junk result"
];
foreach($nameToEyeColor as $key=>$value){
    echo "key:".$key." value:".$value . " ";
}
?>

outputs:

red white blue 
key:0 value:red key:1 value:white key:2 value:blue 
key:beth value:brown key:arnold value:generally bloodshot key:34 value:this is a junk result 

thanks a lot, this helped me a lot

<?php

foreach ($catalog as $item){ echo nl2br($item . "\n");

} foreach ($catalog as $id=>$item){

    echo nl2br($id . "\n");

        foreach($item as $key=>$sub_item){

            echo nl2br($key . " = " .  $sub_item . "\n");
        }

    echo nl2br("\n");
} 

echo nl2br($catalog[101]["title"] . "\n");

foreach ($catalog as $id=>$item){

 echo nl2br($catalog[$id]["title"] . "\n");

} ?>

Array Array Array Array Array Array Array Array Array Array Array Array 101 title = A Design Patterns: Elements of Reusable Object-Oriented Software img = img/media/design_patterns.jpg genre = Tech format = Paperback year = 1994 category = Books authors = Array publisher = Prentice Hall isbn = 978-0201633610

102 title = Clean Code: A Handbook of Agile Software Craftsmanship img = img/media/clean_code.jpg genre = Tech format = Ebook year = 2008 category = Books authors = Array publisher = Prentice Hall isbn = 978-0132350884

103 title = Refactoring: Improving the Design of Existing Code img = img/media/refactoring.jpg genre = Tech format = Hardcover year = 1999 category = Books authors = Array publisher = Addison-Wesley Professional isbn = 978-0201485677

104 title = The Clean Coder: A Code of Conduct for Professional Programmers img = img/media/clean_coder.jpg genre = Tech format = Audio year = 2011 category = Books authors = Array publisher = Prentice Hall isbn = 007-6092046981

201 title = Forrest Gump img = img/media/forest_gump.jpg genre = Drama format = DVD year = 1994 category = Movies director = Robert Zemeckis writers = Array stars = Array

202 title = Office Space img = img/media/office_space.jpg genre = Comedy format = Blu-ray year = 1999 category = Movies director = Mike Judge writers = Array stars = Array

203 title = The Lord of the Rings: The Fellowship of the Ring img = img/media/lotr.jpg genre = Drama format = Blu-ray year = 2001 category = Movies director = Peter Jackson writers = Array stars = Array

204 title = The Princess Bride img = img/media/princess_bride.jpg genre = Comedy format = DVD year = 1987 category = Movies director = Rob Reiner writers = Array stars = Array

301 title = Beethoven: Complete Symphonies img = img/media/beethoven.jpg genre = Clasical format = CD year = 2012 category = Music artist = Ludwig van Beethoven

302 title = Elvis Forever img = img/media/elvis_presley.jpg genre = Rock format = Vinyl year = 2015 category = Music artist = Elvis Presley

303 title = No Fences img = img/media/garth_brooks.jpg genre = Country format = Cassette year = 1990 category = Music artist = Garth Brooks

304 title = The Very Thought of You img = img/media/nat_king_cole.jpg genre = Jaz format = MP3 year = 2008 category = Music artist = Nat King Cole

A Design Patterns: Elements of Reusable Object-Oriented Software A Design Patterns: Elements of Reusable Object-Oriented Software Clean Code: A Handbook of Agile Software Craftsmanship Refactoring: Improving the Design of Existing Code The Clean Coder: A Code of Conduct for Professional Programmers Forrest Gump Office Space The Lord of the Rings: The Fellowship of the Ring The Princess Bride Beethoven: Complete Symphonies Elvis Forever No Fences The Very Thought of You