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

SKU

Hi

I really don't understand this code. why is he adding this to the code? and what is SKU?

foreach($products as $product_id => $product){
     $products[$product_id]["sku"] = $product_id;
}

video ref: duplicating SKU as an Array Element

1 Answer

Hi Don! A SKU (short for Stock Keeping Unit), is a unique identifier for a product. There's a pretty good Wikipedia article that goes into more detail, but the general idea is that it identifies what product someone's talking about uniquely, so that there can't be any confusion.

In that video, Randy starts with an array of products, identified with the SKU as the key. But, he wants to add the SKU to the information for each product as well, to make sure that when iterating through the array he can easily access what the SKU is. The code you included with your question is how we do that. We loop over each item in the products array, and set the "sku" value for each object to equal the key for the array element.

I hope that helps! Definitely feel free to ask any followup questions you have if you need more info!