PHP Build a Basic PHP Website Listing and Sorting Inventory Items Creating the Display Function

jaycode
jaycode
17,744 Points

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!

jaycode
jaycode
17,744 Points

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

Lee Ravenberg
Lee Ravenberg
10,014 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,014 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!