$products[$product_id]["SKU"] VS $product["SKU"]

I want to add the fifth element to second dimension of the $products array.
I have to put $products[$product_id]["SKU"] = $product_id; in the for each loop.
Why can't I put $product["SKU"] instead? isn't every $product equals to $products[$product_id]?

<?php

$products = array();

$products[101] = array(
	"name" => "Logo Shirt, Red",
	"img" => "img/shirts/shirt-101.jpg",
	"price" => 18,
	"sizes" => array("X-Small", "Small", "Medium", "Large")	
);
$products[102] = array(
	"name" => "Mike the Frog Shirt, Black",
    "img" => "img/shirts/shirt-102.jpg",
    "price" => 20,
    "sizes" => array("X-Small", "Small", "Medium", "Large", "X-Large")
);
$products[103] = array(
    "name" => "Mike the Frog Shirt, Blue",
    "img" => "img/shirts/shirt-103.jpg",    
    "price" => 20,
    "sizes" => array("X-Small", "X-Large")
);
$products[104] = array(
    "name" => "Logo Shirt, Green",
    "img" => "img/shirts/shirt-104.jpg",    
    "price" => 18,
    "sizes" => array("X-Small", "Small", "Medium", "Large", "X-Large")
);

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

echo "<pre>";
var_dump($products);
echo "</pre>";
?>

Open in new window

Zoe ZoeAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Dave BaldwinFixer of ProblemsCommented:
Why can't I put $product["SKU"] instead? isn't every $product equals to $products[$product_id]?
No, it's not.  To say $product["SKU"] referers to a field named 'SKU' and you don't have one in your code.  In addition, the code in your foreach section just assigns the $product_id (which is the key for the field) to a field named 'SKU' in the array that already has a 'key' that is $product_id.  I don't know why you want it in two different places.  If you do a 'var_dump' of the results, you will see the $product_id also in a field that is 'SKU' => $product_id.
0
Ray PaseurCommented:
You can learn about PHP arrays here:
http://php.net/manual/en/book.array.php

Here's an example that shows the before-and-after comparison that Dave is describing.
http://iconoun.com/demo/temp_shzoechen.php
<?php // demo/temp_shzoechen.php

/**
 * http://www.experts-exchange.com/questions/28700416/products-product-id-SKU-VS-product-SKU.html
 *
 * REF: http://php.net/manual/en/book.array.php
 * http://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/A_12310-PHP-Variables-and-References.html
 */
error_reporting(E_ALL);
echo '<pre>';

$products[101] = array(
    "name" => "Logo Shirt, Red",
    "img" => "img/shirts/shirt-101.jpg",
    "price" => 18,
    "sizes" => array("X-Small", "Small", "Medium", "Large")
);
$products[102] = array(
    "name" => "Mike the Frog Shirt, Black",
    "img" => "img/shirts/shirt-102.jpg",
    "price" => 20,
    "sizes" => array("X-Small", "Small", "Medium", "Large", "X-Large")
);
$products[103] = array(
    "name" => "Mike the Frog Shirt, Blue",
    "img" => "img/shirts/shirt-103.jpg",
    "price" => 20,
    "sizes" => array("X-Small", "X-Large")
);
$products[104] = array(
    "name" => "Logo Shirt, Green",
    "img" => "img/shirts/shirt-104.jpg",
    "price" => 18,
    "sizes" => array("X-Small", "Small", "Medium", "Large", "X-Large")
);

// SHOW THE PRODUCTS BEFORE MANIPULATING THE DATA
var_dump($products);

// INJECT THE ARRAY KEY INTO THE 'SKU' FOR EACH PRODUCT
foreach ($products as $product_id => $product) 
{
    $products[$product_id]["SKU"] = $product_id;
}

// SHOW THE PRODUCTS AFTER MANIPULATING THE DATA
var_dump($products);

Open in new window

Going forward, you may find that the syntax is easier if you make these nested arrays into objects.  Just a thought -- an array of objects is a very powerful and useful programming structure.
0
hieloCommented:
>> Why can't I put $product["SKU"] instead?
Sure you can, but you need to prefix $product with & (the reference operator):
foreach ($products as $product_id => &$product) {
    $product["SKU"] = $product_id;
}
http://www.larryullman.com/2012/04/25/passing-values-by-reference-in-php-5/

>>isn't every $product equals to $products[$product_id]
No.  $product is a copy of the value at $products[$product_id].
1

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Ray PaseurCommented:
@hielo: Good point.  Variables and references are handled a little strangely in PHP.
http://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/A_12310-PHP-Variables-and-References.html
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
PHP

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.