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 Build a Basic PHP Website (2018) Listing and Sorting Inventory Items Creating the Display Function

Josh Newman
Josh Newman
4,421 Points

Problem with understanding

$catalog[101] = [ "title" => "A Design Patterns: Elements of Reusable Object-Oriented Software", "img" => "img/media/design_patterns.jpg", "genre" => "Tech" ]

What is [101] in this case, what is "title", and what is "A Design Patterns: Elements of Reusable Object-Oriented Software". Thanks! :)

4 Answers

Mike Henry
PLUS
Mike Henry
Courses Plus Student 5,373 Points

$catalog[] is an array - and 101 is the number of the element of the catalog array

"title" => "A Design Patterns: Elements of Reusable Object-Oriented Software" is a key=>value pair with "title" as the key and "A Design Patterns: Elements of Reusable Object-Oriented Software" is the value

so the element 101 contains 3 key value pairs, title has a value, img has a value and genre has a value

Matt Corby
Matt Corby
2,385 Points

Yep! This data combines 2 different types of arrays: Indexed and Associative.

The indexed part is the overall $catalog array since each of it's items can be accessed using a number. For example, when I type $catalog[101], i'm referring to the entire array that starts with "title" => "A Design Patterns: Elements of Reusable Object-Oriented Software".

That 'item' which has all the key => value pairs is an Associative array. You can refer to items in this array by using the key to the data you want. If I type the following:

$associativeArr = $catalog[101]; echo $associativeArr["title"];

Then "A Design Patterns: Elements of Reusable Object-Oriented Software" will be the data that is echoed.

Josh Newman
Josh Newman
4,421 Points

Thanks for the answer! So 101 is just a name for specific $catalog[] array?

Josh Newman
Josh Newman
4,421 Points

Thank you very much, I understand now! :)