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 Simple PHP Application Integrating with PayPal Including the Products Array

Lucas Santos
Lucas Santos
19,315 Points

Don't understand foreach($products as $product_id => $product)

<ul class="products">
                    <?php foreach($products as $product_id => $product) { 
                            echo "<li>";
                            echo '<a href="shirt.php?id=' . $product_id . '">';
                            echo '<img src="' . $product["img"] . '" alt="' . $product["name"] . '">';
                            echo "<p>View Details</p>";
                            echo "</a>";
                            echo "</li>";
                        }
                    ?>
                </ul>

 im having difficulty understanding is the $product_id in foreach($products as $product_id => $product)
 I get that foreach loop goes through the $products array and then you give it a variable after declaring the as. I just don't know if $product_id is a made up variable like $product or is  $product_id set so it automatically gets the id you gave it in the $products array like the 101 in here

$products = array();
$products[101] = array(
Lucas Santos
Lucas Santos
19,315 Points

sorry about putting my entire question in the code box, for some reason it's not letting me separate text and code with the 3 backticks

Don't worry. I just had the same problem responding to your post. Your name sounds so Brazilian by the way.

7 Answers

That is basically it. the "=>" means you want to apply a mask to the foreach loop. It basically tells the interpreter that for each element of the array $products, assign its key to the $cats_id variable and the value corresponding to that key to the $cats variable, no matter what that value is. As I stated before, this value can be another array. In your case you can even have another foreach loop inside this one to loop through the $product array. For this case you'd get the keys "name", "img" and so on. Try running some tests and echoing the result so that you can have a better idea on how it works.

I'll try to explain to you what this syntax does. First of all you need to understand the basic array constructor in php. When you define an array this is one of the possible syntaxes:

     $products = array("key" => "value");

Where the "key" can be a string or an integer and value can be anything, even another array. By taking a look at your code I can see that you have an array of arrays. $products is the main array and it holds several other arrays that relate to an individual product. In this case, the "key" passed to each element is the id of the product and the "value" is another array. Therefore when you write:

```php
 foreach($products as $product_id => $product) {}
You are basically applying a "mask" to it. For each element present in the products array, assign the key to a variable called $product_id and assign the corresponding value to a variable called $product. Another way of understanding this syntax is by writing:

   ```php
     foreach($products as $key => $value) {}

I hope it helps you.

Lucas Santos
Lucas Santos
19,315 Points

Yes this helped but still one part im not understanding now which is the echo '<a href="shirt.php?id=' . $product_id . '">'; if you say that $product_id is equal to the $key of the array elements such as "name", "img", "price" and then it is assigned to a variable that wouldn't make much sense because in the video he got the ID of the individual product instead of the keys. Example:

$products = array();
$products[101] = array(
    "name" => "Logo Shirt, Red",
    "img" => "img/shirts/shirt-101.jpg",
    "price" => 18,
    "paypal" => "9P7DLECFD4LKE",
    "sizes" => array("Small","Medium","Large","X-Large")
);
$products[102] = array(
    "name" => "Mike the Frog Shirt, Black",
    "img" => "img/shirts/shirt-102.jpg",
    "price" => 20,
    "paypal" => "SXKPTHN2EES3J",
    "sizes" => array("Small","Medium","Large","X-Large")
);```

I dont understand how he specifically got the 101 and 102 numbers which is the ID and put it in here  echo '<a href="shirt.php?id=' . $product_id . '">';

Exactly. The $key variable holds the values 101, 102 and so on for each element. The $value variable holds the product itself. Therefore the keys "name", "img", "price", "paypal", "sizes" are keys for the $product variable in your code and the $product_id holds the key of the array element this object was contained within.

Lucas Santos
Lucas Santos
19,315 Points

I see ok so in other words I can do this:

foreach($products as $cats_id => $cats) {}

and it would still get the id 102 in the products array no matter what variable but just because thats the way the foreach loop is set up and it knows that if you use the => the sign before that sign represents the id followed by another random variable that it assigns to.

$products[102] = array(

Lucas Santos
Lucas Santos
19,315 Points

I see ok, I finally get it. Sometimes it gets confusing to know whats a variable and whats a pre defined key word. Still getting the hang of php but it's just keeps getting harder haha

Oh don't worry about it. It gets better eventually. The only thing that gets harder is how to organize your ideas in your head once they start popping up. :)

Lucas Santos
Lucas Santos
19,315 Points

Yeah I really wanna get to the idea part already. I also have to ask when you learn off these tutorials and watch the videos do you just watch and go through the course or do something else that helps you learn better like taking notes or something?

I'm always taking notes of everything. I usually go through the default thing, meaning I watch the videos and take the quizzes as they come, but after this I develop some code and then I try everything I can. I am curious, I write code just to see what is going on behind the scenes. I change variables, logic, everything just to make sure I DO know what exactly is happening. Just focus on what you do the best and go for it but be patient and don't watch all the videos at once. Always code!

Lucas Santos
Lucas Santos
19,315 Points

Always code has probably been the best advice iv gotten lol. I tend to get stuck on these videos and don't end up coding as much as I should. Well thanks for all the help/advice Vinicius, really appreciate it man! (by the way I had to post an answer because for some reason it's not giving me the option to reply to your comment)