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 trialsammalayek
6,743 PointsWhy: $products[$product_id]["sku"] = $product_id;
Inside the {} brackets of the foreach loop in this video, can someone explain
Why it is: $products[$product_id]["sku"] = $product_id;
Instead of: $product[$product_id]["sku"] = $product_id;
2 Answers
Damien Watson
27,419 PointsHey Sam, you would use $products because you are adding a new key-value pair to the parent object, where $product is the value of $product_id and most likely a string.
// so would look something like:
$products["12345678"]["sku"] = "12345678";
sammalayek
6,743 Pointsforeach ($products as $product_id => $product) {
$products[$product_id]["sku"] = $product_id;
}
return $products;
Damien Watson
27,419 PointsDamien Watson
27,419 PointsHey Sam, there is no link to this video. Are you able to paste some code in?