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 Integrating with PayPal Including the Products Array

Having trouble identifying the names of each part of a multi-dimensional array

$suggestions[0] = array( "img" => "img/restaurant.jpg", );

"$suggestions" is the name of my array "[0]" is ?????? "img" is the key to the value on the right "img/restaurant.jpg" is the value to the key on the left

so what is the "[0]"??? In the video I hear Randy refer to it as the "key". So are there 2 keys?

2 Answers

William LaMorie
William LaMorie
11,769 Points

Yes, there are two keys, one for the array, and one an array contained inside that array

An array is a container, and one of the things it can contain is other containers. An array that contains other arrays is called a multi-dimensional array, and the example you are working with is a multi dimensional array. If you're struggling with this, I think analogies are a great place to get clarity:

Think of it like a locker with bags inside of it, or a car with boxes.

for example:

$locker[0] = array("book" => "Headfirst into PHP", "device" => "Amazon Kindle");

$locker[1] = array("sandwich" => "Grilled Cheese", "chips" => "Fried Plantains", "soda" => "Coke");

So the value at index 0 of locker would be an array with two values, one with the key of book and one with the key of device, and the value at index 1 would by an array with 3 values with the keys of sandwich, chips, and soda.

So if I wanted to know what the sandwich was, I could access that like:

$locker[1]["sandwich"];

or the book:

$locker[0]["book"];

notice that when you work with multi-dimensional arrays, the keys start from the outermost array and work in to a smaller one, and these can keep going and going:

So as an example if you had an array named $three_dimensions, and it had an array which had arrays, you could access the first item of the outmost array, 5th item of the middle array, 3rd item of the middle array as such:

$three_dimensions[0][4][2]; //This could represent a 3d game coordinate system

And that's a convention is pretty common throughout a lot of languages. You see very much comparable syntax in Python, Java, JavaScript, C, and so on....

Hope that helps!

'Yes, there are two keys, one for the array, and one an array contained inside that array'.

Got it. I understand how multi-dimentional arrays, just every where I looked for an answer it was referred to as an index, not a key, so I was confused on what to call it. I just wanted to be on the same page as the teacher.

Thank you for your help

Robert Walker
Robert Walker
17,146 Points

The [0] is an index of the array $suggestions

$suggestions = array( 'Robert', 'Carlos', 'Treehouse' );

$suggestions[0] = Robert $suggestions[1] = Carlos $suggestions[2] = Treehouse

The "key" is the first part of a key pair value, so the left side of the =>.

"key" => "value"

"name" => "Robert"

$suggestions = array (

                                   array( 'name' => 'Robert'),

                                   'Carlos', 

                                   'Treehouse' 

                                    );

To access the name Robert now we need to use the index of suggestions and the key to get the value we want like so:

$suggestions[0]['name']

I wrote a couple versions of this I hope this one is simple enough to understand but if not let me know and ill give it another try lol.