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 trialBen Goldman
14,626 PointsPHP 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>
' ' '
4 Answers
Spenser Hale
20,915 PointsGlad you were able to answer your own question.
Rashaad Prince
7,129 PointsAwesome. I was looking at the same exact thing!
Sheri Miner
Courses Plus Student 5,319 PointsYeah I struggled with this at first too. I'm assuming since we use the key which points to [102], [103], etc..... it makes sense.
WCM Operations
Courses Plus Student 15,102 PointsIt was also helpful for me to review the foreach reference page at php.net.
Ben Goldman
14,626 PointsBen Goldman
14,626 PointsOnce again, I answered my own question. I forgot there was an inc file with the keys already set up for the shirts. Never mind.