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 Enhancing a Simple PHP Application Refactoring the Codebase Duplicating SKU as an Array Element

Jerry Schrader
Jerry Schrader
4,396 Points

Confused about variable $product

I am confused by the foreach loop. foreach($products as $product_id => $product) What is the purpose of $product? I am completely confused in this area. Is this : $products[$product_id]["sku"] = $product_id; just altering the original $product_id to add the sku?

1 Answer

The purpose of doing it this way is to get the array index so that it can be added to the array with a key of sku. In this particular case we don't care about the $value portion of the $key => $value pair.