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 (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,017 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,017 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!