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