Heads up! To view this whole video, sign in with your Courses account or enroll in your free 7-day trial. Sign In Enroll
Preview
Start a free Courses trial
to watch this video
There are a few different commands we can use to create arrays in PHP. In this video, we’ll look at new syntax that might make more sense for larger and more complex arrays. We’ll set up the array for our media library catalog using that new syntax. We'll also see how to specify our own key value.
There's another syntax for adding new
elements to an array that will be
0:00
a little easier to maintain as our catalog
grows both larger and more complex.
0:02
So let's go back and check that out now.
0:08
Okay let's go back to
the top of this file.
0:11
And now we're going to add catalog [ ] =,
0:13
and then we'll say Clean Code.
0:18
This tells PHP to add one
new element to the array
0:25
using whatever index is available next.
0:28
PHP will assign an index
just like it did before.
0:31
In this example,
Design Patterns will have an index of 0.
0:34
Forrest Gump will have an index of 1,
and Beethoven will have an index of 2.
0:38
This new item, then, Clean Code,
will have an index of 3.
0:43
Be sure to practice and
get comfortable with these two commands.
0:47
This command creates a new array
with three elements in it.
0:51
This command adds one new array
element to an existing array.
0:55
Let's go ahead and rewrite this for
consistency's sake so
1:00
that each new element gets
added like Clean Code did.
1:03
It's good practice to declare
catalog first as an empty array.
1:07
So we'll start like this,
close off our array.
1:12
And then we'll add catalog, opening and
1:16
closing equals design patterns,
we'll add that.
1:21
Catalog equals and
1:33
catalog equals.
1:38
Now let's finish off these closing
1:44
semicolons and save the page.
1:49
The way we have created our array so far
PHP automatically assigns the key zero for
1:55
the first element, one for
the next, and so on.
2:00
There are some cases where we'll want
a specific key for a particular item.
2:03
So that we can reference
that item by its key leader.
2:08
PHP will allow us to specify the key for
2:12
each element here inside
the square brackets.
2:14
Let's specify 101 as the key for
Design Patterns.
2:18
We could use this for
something like an item ID or
2:22
a SKU or any kind of unique identifier
that we will be able to use
2:24
to reference a particular
item elsewhere in our code.
2:29
To make this easy for
us to identify things at a glance,
2:32
we're going to group each of these types
or categories of items in 100 blocks.
2:35
We gave the book
Design Patterns a key of 101.
2:41
Let's give the movie
Forrest Gump a key of 201.
2:44
And the music of Beethoven a key of 301.
2:48
Since Design Patterns is a book and Clean
Code is a book, let's give Clean Code
2:56
the key of 102, which is the next number
in sequence for the book category.
3:01
When we take a look at
this in the browser,
3:07
we'll see Clean Code added to the list.
3:09
We've covered quite a bit of ground so
far.
3:13
In the next video, we'll move forward
to even more excitement with arrays.
3:15
You need to sign up for Treehouse in order to download course files.
Sign up