What to expect in the next generation of Laravel Forge. Read the blog post
Rewatch this lesson

Next video in… 10

Functions

Arrays

PHP Fundamentals

Arrays
Explore PHP's versatile arrays, which can serve as both numbered lists and key-value stores. Perfect for handling structured data.


Arrays are one of PHP's most powerful features. If you're coming from JavaScript, you'll find them particularly interesting as they combine the functionality of both JavaScript arrays and objects. While PHP also uses objects for holding data (which we'll explore in our Classes video), arrays are incredibly versatile and widely used. Let's explore how they work.

Basic Array Types

In PHP, we have two main types of arrays. First, there are numeric arrays (similar to JavaScript arrays), which use numbers as their keys starting from 0. Then we have associative arrays, which use named keys (similar to JavaScript objects). The great thing about PHP arrays is that they can also mix both styles.

Let's look at some examples:

1// Numeric array
2$colors = ['red', 'blue', 'green'];
3 
4// Associative array
5$user = [
6 'name' => 'John',
7 'age' => 25
8];
9 
10// Mixed array
11$mixed = [
12 42, // index 0
13 'key' => 'value', // key 'key'
14 'another value' // index 1
15];

Creating Arrays

PHP gives us multiple ways to create arrays. While the square bracket syntax [] is most common in modern PHP, you might also see the array() syntax in older code:

1// Modern syntax
2$array1 = ['a', 'b', 'c'];
3 
4// Creating with specific keys
5$user = [
6 'name' => 'John',
7 'age' => 25
8];

Working with Arrays

Accessing array elements is straightforward. For numeric arrays, we use the index number, and for associative arrays, we use the key name:

1$colors = ['red', 'blue', 'green'];
2echo $colors[0]; // outputs: red
3 
4$user = ['name' => 'John', 'age' => 25];
5echo $user['name']; // outputs: John

Arrays can also be nested, allowing you to create more complex data structures:

1$blogPost = [
2 'title' => 'Getting Started with PHP',
3 'author' => [
4 'name' => 'John',
5 'role' => 'editor'
6 ],
7 'comments' => [
8 ['user' => 'Jane', 'text' => 'Great article!'],
9 ['user' => 'Bob', 'text' => 'Thanks for sharing']
10 ]
11];
12 
13echo $blogPost['title']; // outputs: Getting Started with PHP
14echo $blogPost['author']['name']; // outputs: John
15echo $blogPost['comments'][0]['text']; // outputs: Great article!

Common Operations

PHP provides many ways to work with arrays. Here are some operations you'll use frequently

1// Adding elements
2$colors[] = 'yellow'; // Add to numeric array
3$user['email'] = '[email protected]'; // Add to associative array
4 
5// Checking elements
6isset($user['email']); // Check if key exists
7in_array('red', $colors); // Check if value exists
8 
9// Array size
10count($colors); // Get number of elements
11 
12// Remove elements
13unset($user['email']); // Remove specific element

Real World Example

Let's look at a practical example that you might encounter when working with data in a web application:

1$post = [
2 'title' => 'Getting Started with PHP',
3 'author' => [
4 'name' => 'John Doe',
5 'role' => 'editor'
6 ],
7 'tags' => ['php', 'beginner', 'tutorial'],
8 'published' => true,
9 'comments' => [
10 [
11 'user' => 'Jane',
12 'message' => 'Great article!'
13 ],
14 [
15 'user' => 'Mike',
16 'message' => 'Thanks for sharing'
17 ]
18 ]
19];

This structure shows how arrays can handle complex data with different types and nested information - something you'll often use when working with databases, APIs, or configuration files.

00:00
Arrays are one of PHP most powerful features. So if you're coming from
00:04
JavaScript you found them particularly interesting as they both combine the
00:07
functionality of JavaScript arrays and objects. Arrays in PHP are incredible,
00:13
versatile and widely used. So you're going to use them a lot. So let's explore
00:17
how they work together. Basically we have two types of arrays in PHP. Let's start
00:23
with a simple one. This is a numeric array. Let's put in here some colors red
00:28
green and maybe blue. As you can see I'm opening and closing them with
00:35
square brackets. This is how we can create a numeric array. Let's use the
00:40
var dump method again to take a look at what's inside this variable. This
00:46
is what our numeric array looks like. On the right here we have our strings and
00:51
on the left you can see we have keys. The keys are numeric. That's why they
00:55
are called numeric arrays. They start by 0 0 1 and 2. Next to that we also
01:01
have associative arrays. Let's create now a user again with square brackets.
01:06
I'm going to format this a little bit differently because here we have now
01:11
named keys. Let's say the name of this user. Maybe that's just me
01:16
Christoph. Another key, my age. I'm going to recreate this but this is 40. This
01:26
hurts but yeah it's true. Now let's dump out this one. Our user here and you can
01:31
see now on the left we have our named keys with name and age and on the right
01:36
we have our string Christoph with a typo. As always let's fix this and then with
01:43
my age which is an integer. You can also see here we can mix types. PHP
01:48
doesn't matter if you put in a string, an integer, another array, a boolean,
01:53
whatever. You can put in there anything you like. This is also something that
01:57
makes it very versatile. These are the two types of array which we have
02:01
here and you have seen we can create them quite easily with square brackets.
02:05
Now let's see how we can access data. I can dump out the whole array but
02:10
mostly we're interested in maybe a specific value. Let's start with colors
02:14
again. The way we do this is we're going to use square brackets again and now
02:19
inside them we define which element we're interested in. For numeric arrays
02:24
we need to profine the key which is let's go for red which is 0. You can
02:30
see we get red back by the way. This of course also works with a very famous
02:35
method in PHP which is echo which can echo out strings. Now we don't see
02:40
the type anymore and just the string value. If we're interested in green or
02:45
blue, maybe let's go for blue, we would use the key 2. Of course for
02:50
associative arrays this is a little bit different because now we need to
02:53
define the key name. Let's go for the name here and you can see this works
02:58
as well. It's also good to notice here if we have a big array like this one with
03:02
nested values. Let's dump this out first. This is our blog post. You can see we
03:11
have an array with title with the author but then we have another array inside
03:14
the array with the name of the author and the role and then we have comments
03:18
and in the comments we have another array and here we have one element with
03:22
one of our comments for this article with the user and the text.
03:28
If we try to get one of the values inside here of this nested array we
03:32
can do this as well. Let's use echo here again. We're going to start by using
03:37
the variable blog post and then inside we have a named key called comments.
03:43
Inside comments we have one array so this means we have to select the
03:50
specific one. It's the first one here which we get with 0 and now inside this
03:54
array we have named keys again. This means here we can define the user for
03:59
example and we should get backchain and you can see this is also working.
04:04
This is how you can get values from nested arrays. Let's go back to
04:09
our colors example with red, green and blue. Here we go and now let's talk
04:15
about some of the common operations. We already talked about how we can get
04:20
specific values but maybe you also want to add something. We can do this as
04:25
well. We're going to use square brackets again but now we leave them closed and
04:29
this tells PHP we want to add something to the array to the end of the array.
04:34
Let's add another string, another color, yellow in this case and we don't see
04:39
anything because we need to dump this out. Here we go and you can see now we
04:45
have added yellow. Of course as we talked about you could put in here anything, a
04:50
number, you could also put in here another array which in this case is an
04:54
empty one so PHP doesn't matter. You can do all of this but this is how you can
04:58
add something to an array. What else? You're probably going to need often to
05:04
check how many items are inside an array. You can do this with the count method in
05:08
PHP. You can see we have three and most commonly you would use something
05:13
like this inside a loop. For example like this one here. If the count of
05:19
colors, let's say if it's bigger than one, then we're going to echo something out.
05:24
Maybe just a string called true and you can see this works as well. Something
05:30
that's also very common is to use the isSet method to make sure if a variable
05:35
or in this case if a key is set. By the way let's fix this. This looks better and
05:40
we need to get rid of this. We want just to check if it's set we get the string
05:44
true back. If we use something else which is not set here in this scope here you
05:49
can see we don't anything back because this condition is not being matched.
05:53
By the way we have a whole video about conditions so if you're not familiar with
05:57
this we will talk about this in another video. I guess the only and last
06:03
thing that I want to show you maybe also want to remove something from an array.
06:06
The most common approach is to use the unset method and we're going to define
06:11
here our array and then we define the key. For a numeric array of course we
06:16
have to define the number here and maybe let's just dump out colors again and we
06:22
should see now in this case that that red is gone here and yes we only have
06:27
green and blue here. Let's try another one. Let's try two and now blue should be
06:32
gone and yeah this also works. That's basically it for today's video
06:36
about arrays. We talked about how to create arrays, how to add items, remove
06:40
items, how to work with them in conditions, what kind of types we can put
06:45
into arrays here. I guess you have already seen how orthotile and
06:49
powerful arrays are in PHP and trust me you are going to use them a lot.