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

Why: $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;

Damien Watson
Damien Watson
27,419 Points

Hey Sam, there is no link to this video. Are you able to paste some code in?

2 Answers

Damien Watson
Damien Watson
27,419 Points

Hey 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";
foreach ($products as $product_id => $product) {
        $products[$product_id]["sku"] = $product_id;
    }
    return $products;

>>>Video<<<