Bummer! This is just a preview. You need to be signed in with a Basic account to view the entire video.
Associative Arrays3:48 with Alena Holligan
You can assign your own keys to array elements -- and they don't have to be numbers. In fact, your code can be easier to read and understand if you use a name for a key. This is called an Associative Array, because a specific key is associated with a specific value.
Arrays are also referred to as a hash or dictionary
Comma After Elements
The comma after the last array element is optional and can be omitted. This is usually done for single-line arrays, i.e. array(1, 2) is preferred over array(1, 2, ). For multi-line arrays on the other hand the trailing comma is commonly used, as it allows easier addition of new elements at the end.
You can use the extract function to extract the key value pairs as individual variables.
$var_array = array("color" => "blue", "size" => "medium", "shape" => "sphere"); extract($var_array);
Gives us the variables $color = "blue", $size = "medium", and $shape = "sphere".
You can even use spaces to line up the values.
$iceCream = array( 'Alena' => 'Black Cherry', 'Dave' => 'Cookies and Cream', 'Treasure' => 'Chocolate', 'Rialla' => 'Strawberry' );
Shortcut for assigning values
$iceCream = [ 'Alena' => 'Black Cherry', 'Dave' => 'Cookies and Cream', 'Treasure' => 'Chocolate', 'Rialla' => 'Strawberry' ];
The arrays we created so far are all numerically indexed. 0:00 In other words, the first element is at position 0. 0:04 The second element is at position 1. 0:08 PHP automatically assigned the numeric key for us to use. 0:12 But you can assign your own keys to an array element and 0:17 they don't have to be numbers. 0:21 In fact, your code may be easier to read and 0:23 understand if you use a name for a key. 0:25 For example, 0:29 if I wanted to create an array of people's favorite ice cream flavors. 0:30 It would be much easier to just specify their name as the key. 0:34 Alina likes black cherry, Dave likes cookies and 0:38 cream, Treasure likes chocolate. 0:42 This is called an associative array 0:44 because a specific key is associated with a specific value. 0:47 Let's jump into workspaces and see how this works. 0:52 Let's take a look at the results of our indexed array again. 0:56 We see the key on the left with a double arrow separating the value on the right. 1:00 We also use this double arrow to format associative arrays. 1:05 Let's create a new file named associative_arrays.php 1:10 Add the PHP tags. 1:21 And we're ready to start with a fresh array for our favorite ice cream. 1:24 We'll name this iceCream and we'll set it equal to an array. 1:28 This time we want to start by specifying our key. 1:34 We'll start with Alina. 1:38 Then we use the double arrow followed by the value Black Cherry. 1:42 Then just like we did with the indexed arrays, 1:49 we separate the element with the comma. 1:52 Next we'll add Treasure. 1:55 Followed by the double arrow and chocolate. 1:59 Add a comma for separation and we'll add Dave. 2:03 The double arrow and cookies and cream. 2:08 And one more comma, Rialla followed by the double arrow and strawberry. 2:15 This array is getting pretty hard to read. 2:28 I'll [INAUDIBLE] together and wrapping to the next line. 2:30 But PHP doesn't really care about whitespace. 2:33 So often times we can use that to our advantage. 2:36 Let's put each of these elements on its own line. 2:39 Using whitespace can make things easier to read at a glance. 2:51 Now let's var dump this array and run our script. 2:55 Now we see that our key is the name of the person and 3:05 the value is their favorite ice cream. 3:08 We can pull the value of an element by specifying the key. 3:10 Echo $iceCream, 3:16 Alena Now, 3:21 when we run the script, the first thing we see is my favorite ice cream. 3:27 Associative arrays can be extremely useful and provide a lot of information but 3:32 there are a few specifics that you should know about array keys. 3:38 In the next video, 3:42 we'll take a closer look into mixing data types in both array keys and values. 3:42
You need to sign up for Treehouse in order to download course files.Sign up