PHP Build a Basic PHP Website 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
PRO
Mike Henry
Pro 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! :)