Associative Arrays5:30 with Alena Holligan
We will use a multidimensional associative array to store the information for our Media Library Catalog. In this video, we’ll add a second dimension to our current array.
We left our catalog page looking like this with the bulleted list of four items. 0:00 In the code for 0:05 the catalog.php file, we had our catalog stored in a simple indexed array. 0:05 Instead of letting PHP assign the key, 0:10 we have specified our own item ID for the key. 0:12 Each element in the main catalog array represents one item. 0:16 Right now each element has a piece of text with a title of the item as its value. 0:20 Let's change this first element making the value another array 0:25 instead of just a piece of text. 0:30 Let's go back into our catalog. 0:32 Before we add anymore information to our array here, 0:35 let's separate out this code into a new include file. 0:38 We'll name this file data.php. 0:41 Let's copy our array from catalog.php, 0:46 and paste it into data.php. 0:51 Add our opening php tag, and the closing php tag. 0:55 Now we're ready to add a new dimension to our array. 1:03 I'm going to show you another shortcut for adding an array. 1:05 Instead of using the keyword array with parentheses, 1:09 we can simply add open and closed square brackets. 1:12 We'll use this as we add another array to the first element 1:16 in our main catalog array. 1:19 I'll refer to this as our interior array, 1:22 because it's inside our main catalog array. 1:24 This interior array will contain information about this one item. 1:27 One element for each piece of information. 1:32 The first element will contain the title. 1:35 We'll specify the key for each element here using a piece of text to describe 1:38 what information the element contains. 1:43 Title and then the = sign, followed by the > sign. 1:46 Then we also have the value in quotation marks, because it too, is a piece of text. 1:53 We need to add another element to this interior array, for 1:58 all the information about this item. 2:01 We separate items in array with a comma. 2:03 Then we can create a new element for each of the shared attributes of this item. 2:07 The next element we'll add is the path to the image. 2:11 Call it Image=>, our path 2:15 is img/media/design_patterns.jpg. 2:21 Then we'll add the genre. 2:31 Tech. 2:36 The format. 2:39 Paperback. 2:43 And the year. 2:47 Since this value is a number, it does not need quotation marks. 2:52 Finally, let's specify the category. 2:57 This will match up with one of the navigation headings, books, 3:02 movies, or music. 3:05 This one's Books. 3:07 Each category will have some extra details that are specific to the item category. 3:10 Design Patterns is a book, so now we need to add the extra book elements. 3:14 To start with, we'll add ISBN. 3:19 Then we'll add publisher. 3:25 And finally, authors. 3:31 Although many books only have one author, some books, 3:33 including this one, have multiple authors. 3:36 Instead of making authors a string like our elements, 3:39 we'll make authors another array. 3:42 And just like that, we have a third dimension to our associated array. 3:46 Let's add our authors. 3:50 And let's finish off our arrays. 3:59 We've changed the first element in our main catalog array from a simple piece of 4:01 text to a full associative array, this makes our item much more useful. 4:05 We now have attributes for each item grouped together. 4:11 This will allow us to display details of each item. 4:14 To get our catalog ready to use, 4:17 we'll need to add the details of each item in the array. 4:19 For movies and music, we'll have elements specific to those items. 4:22 You probably don't want to watch me type out all the code for all the other items 4:27 in the catalog, and you probably don't want to type them yourself. 4:30 So I've attached a text file to this video with the code you'll need. 4:34 I also added a few more items so we have four items in each category. 4:38 Download that text file and copy the code from that file and 4:42 paste it into our data.php file. 4:46 Also, when ending a php file with php code, 4:52 you do not need to use the php closing tag. 4:55 Leaving off the closing tag is a widely accepted coding practice 4:59 used by projects such as WordPress and Drupal. 5:03 In fact, the Zend Framework specifically forbids using it. 5:06 The closing tag is not required by PHP, and by omitting it at the end of a file, 5:10 you guarantee that no trailing whitespace can be added. 5:16 These might cause unexpected results. 5:20 Once you've pasted your code the full array should look like this. 5:22
You need to sign up for Treehouse in order to download course files.Sign up