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

PHP Arrays and Keys

I've been looking at the following code and trying to understand exactly how the id key is incremented. First, how does the foreach function know to start off at 101? Then, what makes it increment with each succession of the loop? Such as 101, 102, 103, etc. Thanks in advance to anyone who comments.

' ' '

<article>
<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>

</article>

' ' '

Once again, I answered my own question. I forgot there was an inc file with the keys already set up for the shirts. Never mind.

4 Answers

Glad you were able to answer your own question.

Awesome. I was looking at the same exact thing!

Yeah I struggled with this at first too. I'm assuming since we use the key which points to [102], [103], etc..... it makes sense.

It was also helpful for me to review the foreach reference page at php.net.

http://php.net/manual/en/control-structures.foreach.php