Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

PHP

question regarding php array.

As I know in associative array there is a key and value , for example $array1 = array( "title" => "java programming", "category" => "book" );

foreach ($array1 as $key => $value){ echo $key . "=>". $value . "<br/>"; } when iterate over it it will print title as a key and the value as java programming.

What Confused me is I follow the php course and there is array that I confused with what is the key exactly ? for example $catalog[101] = [ "title" => "A Design Patterns: Elements of Reusable Object-Oriented Software", "img" => "img/media/design_patterns.jpg", "genre" => "Tech", "format" => "Paperback", "year" => 1994, "category" => "Books", "authors" => [ "Erich Gamma", "Richard Helm", "Ralph Johnson", "John Vlissides" ], "publisher" => "Prentice Hall", "isbn" => '978-0201633610' ];

Here the key is 101 or for example title? It's really confused me.

2 Answers

Hi Yousif,

This is an example of a multi-dimensional array where you have arrays within arrays.

In your simple example at the top, the values of the array elements were simple strings like "java programming". But the value could be another array. This is what your $catalog example is.

Both 101 and "title" are keys. 101 is a key for the outer array and "title" is a key for the inner array.

The $catalog variable is an array with integer based keys like 101. The value at that key is that long array with all the item information in it.

That inner array contains key, value pairs itself. "title" is one of those keys and the value for that key is the "Design Patterns" title.

Another key in this inner array is "authors". The value for that key is another array. This inner array doesn't specify any keys, only values which are the author names. This means that integer based keys are used and they would start at 0. The indexes 0, 1, 2, 3 would be used as the keys for each author.

Richard Hall
Richard Hall
9,139 Points
$catalog[101] = [ 
  "title" => "A Design Patterns: Elements of Reusable Object-Oriented Software", 
  "img" => "img/media/design_patterns.jpg", 
  "genre" => "Tech", 
  "format" => "Paperback", 
  "year" => 1994, 
  "category" => "Books", 
      "authors" => [ 
        "Erich Gamma", 
        "Richard Helm", "
        Ralph Johnson", 
        "John Vlissides" 
        ], 
  "publisher" => "Prentice Hall", 
  "isbn" => '978-0201633610' 

];
echo "<pre>";

foreach($catalog[101] as $key => $value){
  echo $key . "=>" . $value . "<br>";
}
echo $catalog[101]['title'];

This is my code, showing what the key-value pairs are for the specific $catalog[101] array. If I'm reading your question correctly, I believe the answer to your question is 'title' is the key.

If you were to echo $catalog[101]['format']; for instance, you would be outputting the value assigned to the 'format' key.

changed comment to answer