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 trialA X
12,842 PointsNot Understanding the Difference/Similarities Between a Key and an Array Index...
In the PHP intro to array course Alena mentions that the index in an array is called a key, but I've never heard of or used this term in other courses when referencing arrays. Is key basically synonymous to an array index? Why is the term key used at all? Is there any differences between a key and an array index?
3 Answers
Damien Watson
27,419 PointsHi, in PHP they are the same thing, the index is a key to the values stored against each number. I've never though of it that way until watching this video. The one thing about PHP is that your 'indexed' array may contain 5 values stored against the numbers '(0,1,2,3,4)'.
$basic = array(0,1,2,3,4);
echo var_dump($basic);
// ==> array(5) { [0]=> int(0) [1]=> int(1) [2]=> int(2) [3]=> int(3) [4]=> int(4) }
When you do a 'var_dump
' you can see that they have the key definition (surrounded in []) pointing to the values.
If you unset two from the middle without 're-indexing' your index numbers may now be '(0,1,4)', which makes me think of them more as 'keys'.
unset($basic[2]);
unset($basic[3]);
echo var_dump($basic);
// ==> array(3) { [0]=> int(0) [1]=> int(1) [4]=> int(4) }
You can see in the above example that removing item '2' and '3', leaves '4' at position '4'. In other languages, removing index '2' & '3' would change '4' to position '2'. But because PHP stores them as key/value pairs, it stays as previously defined. You can reindex the array numerically by using 'array_values'.
// $basic = array_values($basic);
You can see the keys clearer if you add your own 'key' value to the same array structure to have keys '(0,1,4,"month")'.
$basic['month'] = 'June';
echo var_dump($basic);
// ==> array(4) { [0]=> int(0) [1]=> int(1) [4]=> int(4) ["month"]=> string(4) "June" }
"Key-Value" pairs is the generic way of storing data, think of variables, database data etc. If you are only using 'indexed' arrays (ie starting at '0' and going from there) then calling them indexes makes sense, but adding 'keys' into it changes it from just 'index'.
Hope this clears it up a bit.
Rita Bradley
7,759 PointsThink of a file cabinet that has any number of drawers, and each drawer has a different lock. To get the content of a drawer, you need you use the key for that particular drawer otherwise, you can't retrieve its contents. It's basically the same concept with why array indices are called keys. You need to use the particular "key" to access that specific content (value) in the array. I hope that made sense.
Damien Watson
27,419 PointsHaven't heard this example of filing cabinet and keys, cool.
<noob />
17,062 PointsThanks for the answer :]
Michael Culler
7,363 PointsMichael Culler
7,363 Pointswhat do you mean by unset? i'm so confused right now.
Damien Watson
27,419 PointsDamien Watson
27,419 PointsI realise this is from way back, but have added examples and cleaned up a bit for better understanding.
Leon Hoffmann
6,620 PointsLeon Hoffmann
6,620 PointsReally good answer Damien!