Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

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.