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

What does $id and $item point to?

I'm having problems understanding this lesson. What I think is ....

$id points to the key in the 1st dimension of the array.... ie.) [101], [201], [301] etc.?

$item points to each key in the 2nd dimension of the array... ie.) 'title', 'img', 'genre', 'format' etc.

I'm getting crossed-up. If anyone can shed some light on this it would be greatly appreciated. Thanks!

Lee Ravenberg , Many Thanks for the explanation! Are my examples correct in my post above?

Lee Ravenberg
Lee Ravenberg
10,018 Points

My pleasure! Your example is almost spot on. $id does not refer to the key in the first dimension of the array. It refers to the key of the second dimension. And $item points to the value of the second dimension.

1 Answer

Lee Ravenberg
Lee Ravenberg
10,018 Points

Hi Jaycode, I will try to explain.

In the video the teacher is looping over array named $catalogue. This is an associative array, meaning that the items inside it are stored in shape of $key => $value pairs.

So the loop is written like this;

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

Each iteration (each time the loop rotates), the $id variable contains a key of the $catalog array, and $item the value.

The reason why this might be confusing, is that the point of writing it down like this is useless at the moment; only the $item is used within the get_item_html() function at the moment. The teacher will probably elaborate on this in the videos that follow so stay tuned!