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 trialJosh Newman
4,421 PointsProblem 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
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
2,385 PointsYep! 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
4,421 PointsThanks for the answer! So 101 is just a name for specific $catalog[] array?
Josh Newman
4,421 PointsThank you very much, I understand now! :)