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 trialKnowledgeWoods Consulting
5,607 PointsArray 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
14,614 PointsI should say, make a test.php file and try it yourself!. But let's say that a value will be overwritten...
KnowledgeWoods Consulting
5,607 Points<?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
14,614 PointsUse var_dump($products) and you will see.. .:)
But it will be 16
KnowledgeWoods Consulting
5,607 Points<?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?
Patrick Gerrits
14,614 PointsIt does here. It shows 0.
KnowledgeWoods Consulting
5,607 PointsIt does not show. Zero is for yellow. I want to echo red.
KnowledgeWoods Consulting
5,607 PointsBut when i wrote $products[]=""; in the starting it shows correctly...Why is it so???
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".
KnowledgeWoods Consulting
5,607 PointsThen how can i echo red here?
Patrick Gerrits
14,614 PointsDo 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.
KnowledgeWoods Consulting
5,607 PointsThis means red is no longer existed anywhere?
Patrick Gerrits
14,614 Points... Yes i said that. If you overwrite something.. the original thing is gone. So red is gone. Forever.
KnowledgeWoods Consulting
5,607 PointsDon't get Hyper Man...I am new to this that's why i purchased this subscription...:)
KnowledgeWoods Consulting
5,607 PointsAnyways...Thanks a lot for your help.
Patrick Gerrits
14,614 PointsI'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....
KnowledgeWoods Consulting
5,607 PointsThanks 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...!!!