Heads up! To view this whole video, sign in with your Courses account or enroll in your free 7-day trial. Sign In Enroll
Well done!
You have completed PHP Arrays and Control Structures!
You have completed PHP Arrays and Control Structures!
Preview
In this video, we'll take a look at some of the inner workings of PHP arrays so that you can get the most from your data. In particular, we'll dig into some of the details of how keys work, and what types of values you can use.
Array Keys
- Are case sensitive.
- Must be unique or they will be overwritten.
- Can either be an integer or a string. (The value can be of any type.)
- Can be a combination of integers and strings. Note: ksort will NOT work if numeric and string keys are mixed together.
Key Casting
- Strings containing valid integers will be cast to the integer type. E.g. the key "8" will actually be stored under 8. On the other hand "08" will not be cast, as it isn't a valid decimal integer.
- Floats are also cast to integers, which means that the fractional part will be truncated. E.g. the key 8.7 will actually be stored under 8.
- Bools are cast to integers, too, i.e. the key true will actually be stored under 1 and the key false under 0.
- Null will be cast to the empty string, i.e. the key null will actually be stored under "".
- Arrays and objects can not be used as keys. Doing so will result in a warning: Illegal offset type.
- If multiple elements in the array declaration use the same key, only the last one will be used as all others are overwritten.
Related Discussions
Have questions about this video? Start a discussion with the community and Treehouse staff.
Sign upRelated Discussions
Have questions about this video? Start a discussion with the community and Treehouse staff.
Sign up
As you've seen so
0:00
far, arrays can be an extremely powerful
way to store a collection of data.
0:01
We can use arrays to group
information that we might
0:06
otherwise store in variables.
0:09
We can store integers, floats,
strings, and booleans.
0:11
We can even store another array
inside the first array but
0:15
let's save that for the next video.
0:19
In this video we'll take a look at some
of the inner workings of PHP arrays,
0:21
so, that you can get
the most from your data.
0:26
In particular, we'll dig into some
of the details on how keys work and
0:29
what types of values you can use.
0:33
Let's start out by taking a closer look
at four noteworthy aspects of array keys.
0:36
First, array keys are case-sensitive.
0:42
So if we try to use the lower
case alena we get an error
0:45
instead of the value because the lower
case key alena does not exist.
0:50
This also means that if I set
alena in lower case equal to,
0:55
Pistachio.
1:05
The capitalized and lower case Alena will
be treated as two separate elements.
1:10
This brings us to the second noteworthy
aspect that is tied closely to the first.
1:15
Keys must be unique or
they will be overwritten.
1:21
For example, if I want to add
a second Dave to this list and
1:25
try to set a new element
with the key of Dave.
1:28
And assign the value cookie dough.
1:39
A second Dave element is not added.
1:47
Instead, it overrides the element
that already has a key of Dave.
1:49
Just like in real life, when you're
talking about two people with the same
1:55
name, you need some other way to keep
track of who you're talking about.
1:59
Sometimes we use a nickname or
a last name.
2:03
If I were to leave one as Dave and
change the other to David,
2:07
That would be two separate elements or
I could add the full name.
2:15
Dave McFarland And Dave Thomas.
2:21
This also would be two separate keys,
and therefore separate elements.
2:31
Next, keys may be a string or an integer.
2:37
Strings that contain only numbers,
will be converted or cast as an integer.
2:41
Float values, numbers including decimals
will be truncated and cast to an integer.
2:46
And boolean values will be cast to
the integer one, for true, and zero,
2:53
for false.
2:58
Let's comment out this var dump and
add a new array.
3:00
We'll name this keys and we'll use
it to demonstrate some type casting.
3:07
We'll set this to an array.
3:11
The first element will have
3:13
a key of the integer 1 => 'a' and
3:19
the second element will
3:27
have the string 1 => b,
3:32
the float 1.5 => c and
3:37
the boolean true =>
3:43
d Now let's var dump this array.
3:47
Because all the keys in this
example are cast to one,
3:58
the value will be overwritten
on every new element and
4:01
the last assigned value d is
the only element left over.
4:05
Finally, you can mix integers and
strings for your array keys.
4:11
I can add a new element to my ice-cream.
4:16
I won't specify a key but
I'll add the value vanilla.
4:27
PHP adds vanilla to the next
available integer for the key.
4:37
In the case of our ice cream array,
all the previous keys are strings.
4:43
So the first available integer is 0.
4:48
Array values on the other hand
don't have as many rules as keys.
4:51
We don't reference
elements by their values.
4:55
So more than one element
may have the same value.
4:58
For example, when I set the favorite
ice cream of Dave McFarland and
5:02
Dave Thomas both to cookies and cream
5:06
Both elements will have
the same ice cream value.
5:13
Also array values can hold any type
of variable, including floats and
5:17
booleans exactly as they would
be stored in any other variable.
5:22
Just like the array keys, each value
may have a separate variable type.
5:27
When I add the element Andrew.
5:32
And I set that equal to true.
5:42
When I run my script,
I see that the value for
5:46
Andrew is actually stored
as the boolean true.
5:49
Unlike keys, array values may
also contain another array.
5:53
This will give us what is known
as a multidimensional array.
5:58
Let's take a little break before we
jump into multidimensional arrays
6:02
in the next video
6:05
You need to sign up for Treehouse in order to download course files.
Sign upYou need to sign up for Treehouse in order to set up Workspace
Sign up