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

Jagmeet Singh
Jagmeet Singh
2,449 Points

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

What is this foreach loop actually doing?

Afrid Mondal
Afrid Mondal
6,255 Points

The foreach usually iterate over arrays,objects and assign the current element's value to the variable. In your case it is iterating over the $catalog and assigning the value of $id (which is the key) to $item variable.

In addition look into this link [http://php.net/manual/en/control-structures.foreach.php]

Hi Jagmeet,

Are you talking about the foreach loop that is inside the array_category() function?

Are you asking what the foreach statement is doing or what the body of the loop is doing?

And are you ok with what the rest of the function is doing outside the loop?

9 Answers

Codin - Codesmite
Codin - Codesmite
8,600 Points
<?php

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

// This is better understood as:

foreach($array as $key => $value) {
}

?>

The above code is a foreach loop that loops through the array $catalog with the row's key as $id and the row's value as $item.

For example using the following associative array:

<?php

$catalog = array(
    "one" => 1,
    "two" => 2,
    "three" => 3,
    "four" => 4
);

foreach($catalog as $id => $item) {
    echo $id . " - " . $item . "<br>";
}

?>

The foreach would output:

one - 1

two - 2

three - 3

four - 4

Ashley, that was beautifully explained, and exactly what I needed to know...thank you

Jagmeet Singh
Jagmeet Singh
2,449 Points

Thank you Afrid and Jason,i do know what is this foreach loop doing,my only concern is what is the purpose of $id => $item?

Does Ashley's answer clear it up for you or do you need a more specific explanation using data from this project?

Jagmeet Singh
Jagmeet Singh
2,449 Points

Thank you Ashley and Jason!. Yes it helped!

Jagmeet Singh
Jagmeet Singh
2,449 Points

Thank you Ashley and Jason!. Yes it helped!

Catelinn Xiao
Catelinn Xiao
4,139 Points

Sorry I still don't quite understand why $id is needed here. As I tried the function with $item parameter only and called it, the result is the same.

Hi Catelinn,

Are you talking about a function here? This question was about the foreach loop.

Could you clarify what you're asking or post the relevant code?

Catelinn Xiao
Catelinn Xiao
4,139 Points

Hi Jason, I don't understand why when we create the get_item_html function, we need to have $id as parameter as well, as the code in this function will use $item parameter only. With this thinking, I created get_item_html($item) function and called it in the foreach loop as below:

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

It worked with the same result as suggested in the tutorial. So I want to know why to $id is required to pass into the get_item_html function.

Thanks!

Ok, I see. I think you're at the part of the course when you create this function?

I recommend that you change the code back. The $id is not needed right now but will be later.

Your question was asked earlier and so I'll go ahead and link to that so that we don't go off topic here.

https://teamtreehouse.com/community/function-getitemhtml-works-fine-without-id-argument

Let me know if that helps you.

Thank you everyone for both the questions and answers. Very helpful clarification!

--foreach($catalog as $id => $item) Even after reading these answers It took me a minute to figure it out, so I'll just share my answer in case somebody else gets stuck as I did.

--Take a look in your: data.php file Look at the structure of your array:

$catalog = [];
$catalog[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" => [
        "Erich Gamma",
        "Richard Helm",
        "Ralph Johnson",
        "John Vlissides"
    ],
    "publisher" => "Prentice Hall",
    "isbn" => '978-0201633610'
];

The foreach($id => $item) is processing the values like this:

1) The data being passed to `$id` is the key [101] and 
2) the data being passed to `$item` is the value:
[101] => [ "title" => "A Design Patterns: ...",  ... ]
[
"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" => [
        "Erich Gamma",
        "Richard Helm",
        "Ralph Johnson",
        "John Vlissides"
    ],
    "publisher" => "Prentice Hall",
    "isbn" => '978-0201633610'
];

--That's why you can access the keys of the inner associative array like this in your get_item_html() function:

$item["title"]

--And the output would be:

A Design Patterns: Elements of Reusable Object-Oriented Software

I hope this helps.