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

Jake Schrader
Jake Schrader
3,084 Points

Misunderstanding about Foreach

This is something that goes back to a concept that was covered a good few lessons ago, but only now is my lack of understanding starting to bite me in the butt. I'm sure its just a gross misunderstanding of foreach loops, but I don't know.

Relevant code (from both catalog and functions pages)

<?php

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

function get_item_html ($id, $item) {
  $output =  "<li><a href='#'><img src='" 
      . $item["img"] . "' alt='"
      . $item["title"] . "' />"
      . "<p>View Details</p>"
      . "</a></li>";

  return $output;

}

?>

To me, it seems like $id serves no function in the function, so to speak. Nowhere in get_item_html does it reference that arguement, only $item. So why when I remove $id from both code snippets, do pictures and titles no longer load?

This seems simple, but it doesn't evaluate in my head, and is causing me to not understand WHY those arguments need to be passed, and thus not understand how to build a catalog this way.

Jake Schrader
Jake Schrader
3,084 Points

Apologies, it appears the forum is attacking the linebreaks in my post, so it might be somewhat hard to read.

1 Answer

Jake,

PHP's foreach can be a hassle to work with since you can use it two separate ways. It's okay if you are having problems understanding it. Let me simplify this for you:

Foreach is being called by the catalog array. Now the catalog array is a multidimensional array... meaning that instead of simply having an array key with an array value, you now have an array key with another array as the initial array's value. Here is an example below:

$catalog[1001]['title'] = 'BookA';

You can see that the array looks like it has two keys which it does. So logically, you can say that $catalog[1001] is an array in it's own right. Now back to the foreach loop. You are using foreach on the array catalog and extracting both the key and it's value. In this instance, you are actually extracting the array's key and its inner array as the value. This is why when you assign the new array to the variable item, you can use $item['title']. You haven't displayed the whole get_item_html function all the way which shows you what the id is being used for. Remember, all medias are assigned an overall number (101, 102, 201, 202, 301, 302) and that keeps track of the item overall. The inner array is where the actual information about the item is stored. The id variable is being used in the function to display the id that the details.php page will use to retrieve the item to display:

function get_item_html($id,$item) {
    $output = "<li><a href='details.php?id="
        . $id ."'><img src='" 
        . $item["img"] . "' alt='" 
        . $item["title"] . "' />" 
        . "<p>View Details</p>"
        . "</a></li>";
    return $output;
}

Hope this helps you better understand things.

Cheers!

Jake Schrader
Jake Schrader
3,084 Points

Incredible. Actually, it turns out I hadn't added the line with $id into my function at all, so that was why I was confused, LOL. Well, there goes a couple hours of my life.

Thank you greatly for taking the time to answer!