Multidimensional Arrays6:14 with Alena Holligan
Multidimensional arrays allow me to create an nested array of information for each item in my list. In this video we'll take a look at the format for creating multidimensional arrays.
In a multidimensional array, a key in the outer array contains a secondary inner array. Multidimensional arrays may have indexed or named keys.
$task1 = array( 'title' => 'Laundry', 'priority' => 2, 'due' => '', 'complete' => true, ); $taskList = array($task1); $taskNames['Task 1'] = $task1;
In the code above $taskList['title'], $taskNames['Task 1']['title'] and $task1['title'] all return 'Laundry'.
We've seen how arrays can be used to group information and 0:00 store multiple pieces of information in one variable. 0:03 Multi-dimensional arrays take this concept to the next level. 0:07 Our first array was a list of things to learn, each item was stored 0:12 as a simple string but what if I wanted to track more information about these items. 0:16 Maybe, I'd like to add a priority to each item, so, 0:21 that I know which item I need to complete first. 0:24 I might also want to set a due date for an item or 0:27 maybe I want to keep my items in a list and just mark the item as complete. 0:30 Multi-dimensional arrays allow me to create 0:34 a nested array of information for each item in my list. 0:37 Let's take a look at the format for creating multi-dimensional arrays. 0:42 Let's add a new file named todo.php Adding the opening and closing PHP tags. 0:48 To start, I'm going to create a single task as an associative array of details. 1:00 Task one equals an array. 1:06 The first element, title double arrow Laundry. 1:09 The second element, priority. 1:17 Double arrow 2. 1:21 Due double arrow an empty string and complete. 1:22 Will equal the Boolean true. 1:34 When writing an array on multiple lines just like we did, 1:38 the common practice is to end with a comma making it easy to add the next element. 1:42 Let's var_dump task1. 1:47 And then run our script. 1:52 Okay, so we have our first task as an associative array. 1:55 Let's add a second task. 2:00 This title will equal clean out refrigerator. 2:08 We'll set the priority to 3. 2:18 And this will be do on 7/30, 2:22 2016 The complete status. 2:29 Will be false. 2:40 Now let's add both these tasks as elements to a list. 2:44 Task one and task two. 2:53 Now let’s var dump our list. 2:58 Pull the console way up and run the script. 3:02 As you can see from the var dump, 3:07 the first array list has two elements, starting with a zero index key. 3:09 The first element is an array of four items. 3:15 Each of these items has a specific string key 3:19 describing which detail the item contains. 3:23 Let's comment out this var dump. 3:27 If I want to access the title of my first task. 3:32 I use echo list. 3:36 The key of zero for my first task followed by the title for my inner array. 3:40 Now when I run my script I see the title for my first task Laundry. 3:48 Let's take a look at a few other formatting options. 3:52 Since I know that I'm going to add these tasks to my task list. 3:56 I can remove the extra variables and 3:59 add these tasks to my list right from the start. 4:02 I just list add an element that's an array list 4:07 add my second element that is also an array. 4:12 I no longer need this line. 4:19 As of PHP five four, you can also use the short array 4:24 syntax which replaces array and parentheses with the square brackets. 4:27 Open and close. 4:35 Open and close, 4:37 you'll often see this short array a syntax used, when creating multi-dimensional arrays, 4:44 or even associative arrays, whichever syntax you use. 4:49 Spacing is key for helping you make sure that you open and 4:53 close all the symbols properly. 4:56 Let's use this var dump and run the script again. 4:58 Pulling my console all the way up, and 5:05 we see that the structure of the array hasn't changed at all. 5:07 I could also specify the keys for 5:12 my first array by adding a key to the first square brackets. 5:15 Task one. 5:20 Task two. 5:26 There's not really a good reason to specify a key here so 5:33 I'm going to change that first level back to a zero index array. 5:36 Multi-dimensional 5:39 arrays can be a complicated concept to grasp. 5:50 I encourage you to re-watch this video and 5:53 also experiment with some multi-dimensional arrays of your own. 5:55 Also make sure you check out the teacher's notes for more resources. 6:00 Before we conclude this section on arrays 6:05 will take a look at one more aspect that makes arrays a powerful tool. 6:07 The ability to sort. 6:12
You need to sign up for Treehouse in order to download course files.Sign up