WEBVTT

00:00.500 --> 00:03.680
Okay, so let's do a deeper dive on arrays.

00:03.710 --> 00:11.510
Now we've been using arrays previously, but now in this video we are just gonna focus on them entirely.

00:11.510 --> 00:13.880
So we've got a simple array here.

00:14.030 --> 00:20.900
Um this is just indexed automatically by PHP starting with zero.

00:20.900 --> 00:30.410
And then another example is an associative array where we choose the keys and assign values to them.

00:31.520 --> 00:34.040
Now both arrays are or.

00:34.040 --> 00:37.160
The values from both arrays are read the same way.

00:38.120 --> 00:45.950
So you either use a number index like zero, or you use the key from the associative array.

00:45.980 --> 00:48.590
This key needs to be defined there.

00:48.620 --> 00:52.100
So in this case it's for example name.

00:52.100 --> 00:58.760
And when I run this we see one because that's the first element in this array.

00:58.760 --> 01:03.080
And we see John because this is under the name.

01:05.210 --> 01:08.420
Next up to add elements to an array.

01:08.540 --> 01:12.200
Let's start with the simple indexed array.

01:12.650 --> 01:19.220
You just use this syntax square brackets next to the variable name next to the array.

01:19.610 --> 01:21.680
And you can add anything.

01:21.680 --> 01:28.670
Let's say we add six elements are always added at the end of this indexed array.

01:28.910 --> 01:35.630
Now with associative array which is a little bit different you need to choose the key.

01:36.680 --> 01:42.230
So we can add a country which can be USA.

01:44.840 --> 01:49.370
So in PHP you can also have multi-dimensional arrays.

01:51.080 --> 01:58.940
So we can just add an array inside an array like this matrix that I've created in here.

02:01.430 --> 02:09.590
Though it's also possible that inside an indexed array like this one, you can add an associative array

02:10.730 --> 02:14.840
and you access the elements using square brackets.

02:16.430 --> 02:20.150
So this is getting the second array from matrix.

02:20.150 --> 02:27.380
And from this array I'm also getting the second element which should display five.

02:27.380 --> 02:29.780
So let me comment out those.

02:29.780 --> 02:32.270
So we clearly see the result.

02:32.270 --> 02:35.570
So I expect to see five on the screen.

02:35.570 --> 02:36.740
And there it is.

02:38.150 --> 02:38.450
Okay.

02:38.450 --> 02:42.320
So next up let's see some functions that we can use with arrays.

02:42.320 --> 02:44.750
So we've got this fruits array.

02:45.140 --> 02:50.330
We can count them using the count function.

02:52.880 --> 02:55.070
So it should display three.

02:55.730 --> 02:58.480
And then we can sort arrays.

02:58.510 --> 03:05.560
So with sorting, you need to know that sorting works in place, which means the original array would

03:05.560 --> 03:06.820
be modified.

03:09.640 --> 03:16.930
So let's vardump this and the sort function is doing an ascending sort.

03:16.990 --> 03:21.070
So to have a descending sort you do a sort.

03:21.850 --> 03:26.050
Again this also modifies this variable.

03:26.560 --> 03:28.870
It doesn't return a new one.

03:29.050 --> 03:29.350
Okay.

03:29.380 --> 03:31.570
So let's also vardump this.

03:31.600 --> 03:37.210
I'm going to comment out this line and let's run this script.

03:37.420 --> 03:39.910
So the count of elements is three.

03:39.940 --> 03:48.640
Then the elements when sorted in ascending order we've got this fruits and then sorted in descending

03:48.640 --> 03:49.300
order.

03:49.330 --> 03:50.710
That's the order.

03:51.820 --> 03:52.120
Okay.

03:52.120 --> 03:55.210
Now let's do something with associative arrays.

03:55.210 --> 03:59.080
First let me dump the associative array.

03:59.110 --> 04:00.730
Um, what's the name of it?

04:00.760 --> 04:03.670
Okay, that's just associative array.

04:04.840 --> 04:14.560
Now you can also sort the associative arrays, but here we can sort the elements by key or by value.

04:14.590 --> 04:15.430
Okay.

04:15.490 --> 04:19.780
Um a sort will sort by value.

04:23.230 --> 04:24.010
Okay.

04:24.010 --> 04:25.570
Let's vardump.

04:25.570 --> 04:28.420
This also sorts the array in place.

04:28.420 --> 04:31.390
So it is modifying the original variable.

04:32.170 --> 04:34.510
Then we've got k sort.

04:35.380 --> 04:39.070
This as you can guess is sorting by keys.

04:42.250 --> 04:46.240
Let's also vardump that one.

04:48.310 --> 04:49.180
Okay.

04:49.180 --> 04:50.620
Let's run it.

04:51.640 --> 04:59.590
So that's the original array in the order that we have defined that we've also added a country in at

04:59.590 --> 05:00.580
one place.

05:01.150 --> 05:04.630
Then we sorted using a sort.

05:04.630 --> 05:08.170
So this was sorting by value.

05:08.680 --> 05:09.850
That's the result.

05:09.880 --> 05:13.750
And this here is sorting by key.

05:14.410 --> 05:18.280
As you can see keys seem to be sorted alphabetically.

05:20.500 --> 05:20.770
Okay.

05:20.770 --> 05:23.620
So next up let's create some numbers.

05:24.880 --> 05:28.600
So the range function well this one is useful.

05:28.630 --> 05:34.360
You can just generate an array of numbers from 1 to 5.

05:34.390 --> 05:35.860
That's obviously an example.

05:35.860 --> 05:38.470
You can choose whatever numbers you want.

05:39.850 --> 05:42.940
So let's vardump this one.

05:44.110 --> 05:50.770
And also I'd like to make sure that we don't have the screen too cluttered.

05:50.770 --> 05:53.020
So I'm going to comment those out.

05:53.980 --> 05:55.420
So let's run it.

05:55.750 --> 05:58.150
This is our array of numbers.

05:58.150 --> 06:02.200
As you can see, it is indexed starting from zero.

06:04.090 --> 06:09.370
Then let's do something with this array so we can use some functions.

06:10.090 --> 06:14.530
So I think we've already had this example of squaring the numbers.

06:14.530 --> 06:16.720
But let's have it again.

06:16.720 --> 06:19.030
So we have array map function.

06:19.030 --> 06:23.950
Now this will return a new array and we can pass a callback to it.

06:23.950 --> 06:31.480
So we are passing an arrow function that will do something to every element from the array.

06:37.450 --> 06:39.640
So we square the number.

06:39.910 --> 06:44.860
And we also need to pass the array to this function.

06:45.490 --> 06:46.360
Okay.

06:46.360 --> 06:49.180
Now let's vardump the results.

06:52.630 --> 06:54.130
And let's run it.

06:55.810 --> 07:02.020
So that's the original one and that's the results array.

07:04.750 --> 07:05.380
All right.

07:05.410 --> 07:09.490
Now another one is an array filter.

07:10.750 --> 07:19.090
So let's create an array of even numbers and use array filter to only get the even numbers from either

07:19.090 --> 07:21.190
numbers or squared array.

07:21.250 --> 07:23.260
I'm going to go with numbers.

07:23.290 --> 07:30.850
Now as you can see, PHP is not really consistent with the order of arguments.

07:31.090 --> 07:38.590
So with array map the array came second with array filter it comes first.

07:39.370 --> 07:41.200
This is just how it is.

07:41.200 --> 07:43.690
Nothing we can do about this.

07:43.900 --> 07:44.380
Okay.

07:44.380 --> 07:46.990
Let's have an arrow function here.

07:47.020 --> 07:53.200
This is function n and I think we've seen that before.

07:53.200 --> 07:59.670
So we use the modulo operator and we get the remainder of dividing by two.

07:59.700 --> 08:04.260
If it happens to be zero, this is an even number.

08:04.890 --> 08:07.860
Never forget the semicolon.

08:09.690 --> 08:13.260
And why won't we vardump this?

08:13.260 --> 08:17.370
To see the results and all those functions.

08:17.400 --> 08:17.730
Array.

08:17.730 --> 08:18.300
Filter.

08:18.330 --> 08:19.350
Array map.

08:19.380 --> 08:27.000
They will produce new arrays, so they just don't modify the original array that you've passed to it.

08:27.030 --> 08:30.570
Typically this only happens with sorting.

08:30.570 --> 08:32.310
So let's run this one.

08:32.310 --> 08:36.300
And here we have it from the original numbers array.

08:36.330 --> 08:39.360
Only two and four are even numbers.

08:41.310 --> 08:41.550
Okay.

08:41.580 --> 08:45.120
Next up let's see an array reduce function.

08:45.120 --> 08:48.270
So this might be a tough one.

08:48.270 --> 08:51.630
So let's make sure you focus on this one.

08:52.320 --> 08:54.330
So we call array reduce.

08:54.330 --> 09:00.690
Now the goal of array reduce is to reduce the array to one single value.

09:01.920 --> 09:05.640
So in this case first argument would be the array.

09:06.120 --> 09:07.950
Next up would be a function.

09:07.950 --> 09:09.630
I'm going to add it in a second.

09:09.630 --> 09:13.560
And there is also a third argument which is the initial value.

09:13.590 --> 09:21.660
Since we want to reduce that the array to one single value, we also start with an initial value, which

09:21.690 --> 09:27.690
in this case is zero, because we want to calculate the sum of numbers.

09:27.690 --> 09:29.580
So we start with zero.

09:29.580 --> 09:34.530
And now it's important how the callback function looks like.

09:34.560 --> 09:38.100
Let me move every single argument into a separate line.

09:39.720 --> 09:42.870
So your function will receive two arguments.

09:42.870 --> 09:48.450
The first one is called carry and the second one is the element from the array.

09:48.480 --> 09:51.420
Now you don't have to call this argument carry.

09:51.420 --> 09:52.500
It's up to you.

09:52.500 --> 10:00.180
But this is how this is called in the array reduce function, which is not unique to PHP.

10:00.540 --> 10:06.540
And the way it works is we go over every single number from the array.

10:06.570 --> 10:10.650
So if we take a look in here we've got five of those.

10:10.860 --> 10:17.250
And you should return a single value from this callback.

10:17.280 --> 10:25.770
So in our case if we want to calculate the sum, we return the value that was carried from the previous

10:25.770 --> 10:30.120
run or the initial value of that was passed to array reduce.

10:30.120 --> 10:37.830
So this one, when this callback is called for the first time plus n okay.

10:37.830 --> 10:40.860
So with the first call we've got an initial value.

10:40.860 --> 10:43.050
So carry would be zero.

10:43.050 --> 10:45.270
And first element is one.

10:45.270 --> 10:47.280
So we return one from it.

10:47.280 --> 10:52.890
But on the second element of this number's array Carrie is now one.

10:52.890 --> 10:58.290
So we add 1 to 2 and you see where I'm going.

10:58.290 --> 11:00.060
So let's vardump this.

11:00.060 --> 11:05.760
And this also doesn't modify the original array.

11:05.880 --> 11:09.540
And the sum is 15.

11:12.360 --> 11:12.690
Okay.

11:12.690 --> 11:15.060
So now let's see something new.

11:15.300 --> 11:17.280
The array unpacking.

11:17.310 --> 11:21.990
I think this was only introduced in PHP 7.4.

11:22.650 --> 11:26.640
So I'm going to define a more numbers array.

11:26.910 --> 11:29.940
So array is always defined by square brackets.

11:29.940 --> 11:31.770
I'm going to add one value to it.

11:31.770 --> 11:35.610
And then I'm going to unpack the numbers array.

11:35.610 --> 11:39.030
And also I will add something at the end.

11:39.090 --> 11:46.050
So the way it works does a three dot operator before an array doesn't have to be variable.

11:46.050 --> 11:50.570
You can just add an array directly in here like that.

11:53.030 --> 12:00.290
So this operator in this case will just move all the elements from this numbers array into this more

12:00.290 --> 12:01.460
numbers array.

12:02.720 --> 12:04.220
Let's see this in practice.

12:04.220 --> 12:08.720
And I think everything would be clear what's happening in here.

12:10.700 --> 12:11.990
Let's run it right now.

12:11.990 --> 12:13.880
And this is our new array.

12:13.880 --> 12:21.920
So the element that I've added everything that was unpacked from numbers array and this one that we

12:21.920 --> 12:23.900
have added at the end.

12:24.080 --> 12:24.650
Okay.

12:24.680 --> 12:26.840
So this is array unpacking.

12:27.290 --> 12:27.560
Okay.

12:27.590 --> 12:31.610
So next up let's see array destructuring.

12:32.150 --> 12:34.220
So remember our fruits array.

12:34.250 --> 12:35.570
Let's use it.

12:36.140 --> 12:46.160
And using this assignment I want to get the first and second element of the arrays and assign them to

12:46.190 --> 12:47.570
a variable called first.

12:47.570 --> 12:49.940
And a variable called Second.

12:50.030 --> 12:53.450
So let's see what's the result of this.

12:55.970 --> 13:00.680
So this should be some fruits but I'm not sure which ones.

13:00.710 --> 13:01.190
Okay.

13:01.190 --> 13:04.910
So the value of first variable is apple.

13:05.030 --> 13:07.310
The value of second is banana.

13:07.310 --> 13:09.320
So they seem to be sorted.

13:09.350 --> 13:11.870
I can also skip elements.

13:11.870 --> 13:16.700
So uh sorry not here I can skip elements in here.

13:16.700 --> 13:22.160
So actually the variable second would contain the third element.

13:23.030 --> 13:24.830
So definitely not banana.

13:24.860 --> 13:27.770
Now this is orange okay.

13:28.220 --> 13:30.140
Anyway this is possible.

13:33.320 --> 13:33.830
Okay guys.

13:33.830 --> 13:39.200
So I was thinking about going through arrays in one go.

13:39.200 --> 13:41.600
But I think this would be too long.

13:41.600 --> 13:48.680
So let's have a short break and go back to more arrays in the next video.
