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 Build a Simple PHP Application Listing Inventory Items Creating the Products Array

Array Key Confusion

What if i assign 101 key to the first one then leave the second one empty and then assign 102 to the third then 104 to the fourth and leave the fifth one. What will the values of the empty one.

16 Answers

Patrick Gerrits
Patrick Gerrits
14,614 Points

I should say, make a test.php file and try it yourself!. But let's say that a value will be overwritten...

<?php $products[]="red"; $products[10]="blue"; $products[15]="green"; $products[]="yellow";

echo $products[]; ?>

This is my test file. How can i echo yellow here???

Patrick Gerrits
Patrick Gerrits
14,614 Points

Use var_dump($products) and you will see.. .:)

But it will be 16

<?php $products[]="red"; $products[10]="blue"; $products["aa"]="green"; $products[0]="yellow";

echo $products[0] ; var_dump($products); ?>

Var_dump is not showing anything about red?

It does not show. Zero is for yellow. I want to echo red.

But when i wrote $products[]=""; in the starting it shows correctly...Why is it so???

Patrick Gerrits
Patrick Gerrits
14,614 Points

-sigh-

Because you changed your code in your opening post and ind your example.

But lets go again

<?php 
$products[]="red"; $products[10]="blue"; $products["aa"]="green"; $products[0]="yellow";

echo $products[0] ; var_dump($products); 
?>

first 0=.>red. then 10=>blue then aa=>green but then you OVERWRITE 0 to yellow.

So when var_dump or echoing. products[0] is"yellow".

Then how can i echo red here?

Patrick Gerrits
Patrick Gerrits
14,614 Points

Do you even read what i answer to you?

"but then you OVERWRITE 0 to yellow."

So red is not there anymore. The array 0 has become yellow.

This means red is no longer existed anywhere?

Patrick Gerrits
Patrick Gerrits
14,614 Points

... Yes i said that. If you overwrite something.. the original thing is gone. So red is gone. Forever.

Don't get Hyper Man...I am new to this that's why i purchased this subscription...:)

Anyways...Thanks a lot for your help.

Patrick Gerrits
Patrick Gerrits
14,614 Points

I'm getting hyper? I'm using my free time on a sunday to help you. I have given the answer 4 posts ago, but you refuse to read. And then i get annoyed.

By the way, you are not paying me the $30 dollars a month. I do this to help people. I fact i pay money to help you... Wow.. that just hit me. That's.... weird....

Thanks Patrick so much for your help... I was not aware of all that you told me...I thought someone from Treehouse was helping me...RESPECT for you Man...!!!