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 trialJagmeet Singh
2,449 Pointsforeach($catalog as $id => $item)?
What is this foreach loop actually doing?
Jason Anello
Courses Plus Student 94,610 PointsHi 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
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
john larson
16,594 PointsAshley, that was beautifully explained, and exactly what I needed to know...thank you
Jagmeet Singh
2,449 PointsThank you Afrid and Jason,i do know what is this foreach loop doing,my only concern is what is the purpose of $id => $item?
Jason Anello
Courses Plus Student 94,610 PointsDoes Ashley's answer clear it up for you or do you need a more specific explanation using data from this project?
Jagmeet Singh
2,449 PointsThank you Ashley and Jason!. Yes it helped!
Jagmeet Singh
2,449 PointsThank you Ashley and Jason!. Yes it helped!
Catelinn Xiao
4,139 PointsSorry 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.
Jason Anello
Courses Plus Student 94,610 PointsHi 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
4,139 PointsHi 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!
Jason Anello
Courses Plus Student 94,610 PointsOk, 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.
Catelinn Xiao
4,139 PointsGot it. Thanks.
Robyn Gamm
4,408 PointsThank you everyone for both the questions and answers. Very helpful clarification!
Sergio Pichardo
24,963 Points--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.
Cristian Lo Iacono
1,943 PointsYou saved my life. Thanks.
Afrid Mondal
6,255 PointsAfrid Mondal
6,255 PointsThe 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]