WEBVTT

00:00.500 --> 00:00.830
Okay.

00:00.830 --> 00:03.080
Now let's define those two arrays.

00:03.080 --> 00:06.020
Those will be just a set of numbers.

00:06.020 --> 00:10.880
And we're going to calculate the intersection of those two arrays.

00:10.880 --> 00:16.370
So the common elements and then the differences between those two arrays.

00:16.550 --> 00:21.410
First to get the intersection we call array intersect.

00:22.070 --> 00:24.350
And we pass those two variables.

00:24.350 --> 00:32.630
Now with intersection the order you pass the arrays does not matter because the common elements set

00:32.660 --> 00:35.090
would be always the same.

00:36.440 --> 00:43.280
Now there is also an array div function which is used to find the differences.

00:43.280 --> 00:53.720
So if I pass set one and set two, this means that I would like to see all the elements that are present

00:53.720 --> 01:02.640
in set one, but are not present in set two if I invert the order, this will actually give me a different

01:02.640 --> 01:12.540
result, because here I'm interested in elements that are in the set two but are not in set one, and

01:12.840 --> 01:16.050
those are a little bit different.

01:16.050 --> 01:18.090
They don't overlap.

01:19.530 --> 01:27.210
So first if we run that we can see that for the intersection we've got the same results.

01:27.210 --> 01:28.560
This makes sense.

01:28.560 --> 01:35.460
The common elements would always be the same but with the different elements.

01:36.120 --> 01:38.310
First we've got one and two.

01:38.340 --> 01:40.410
These are not present in this set.

01:40.410 --> 01:45.750
And if we invert the order of parameters we've got six and seven.

01:46.590 --> 01:51.480
So this makes sense that those numbers are not present in the first set.

01:53.490 --> 01:53.790
Okay.

01:53.790 --> 01:55.830
Now keys and values.

01:55.830 --> 02:01.210
So let's go back to this associative array we've defined at the top.

02:02.170 --> 02:09.130
And in PHP, you've got a way to just get all the keys and all the values of a specific associative

02:09.130 --> 02:12.100
array, which would produce a new array.

02:12.430 --> 02:14.140
So that's pretty simple.

02:14.140 --> 02:16.600
You do it using functions.

02:17.290 --> 02:20.320
First we've got array keys.

02:21.010 --> 02:23.470
This will just give us all the keys.

02:25.840 --> 02:33.880
And then we've got values which we retrieve using array values function.

02:35.410 --> 02:38.800
Now this is actually quite useful.

02:39.730 --> 02:42.550
So let me dump both.

02:46.030 --> 02:48.040
So here is the result of that.

02:48.040 --> 02:52.660
And those two functions are actually pretty handy.

02:52.840 --> 03:00.170
For example with the keys I can use that to label the table headers.

03:00.170 --> 03:13.130
If I would like to present the data in some kind of a table so I can get the keys, but then I can capitalize

03:13.130 --> 03:14.660
the first letter.

03:15.140 --> 03:17.360
So I've got array keys here.

03:17.360 --> 03:20.240
Maybe let me show you another example.

03:20.240 --> 03:23.150
So I can do array map.

03:25.730 --> 03:37.550
Then with every key I'm going to do you see first key and pass the array of keys to it.

03:39.830 --> 03:42.140
And let me run that again.

03:42.920 --> 03:48.620
And now I've got nicely formatted labels or table headers.

03:51.530 --> 03:54.320
So next up we've associative arrays.

03:54.320 --> 03:54.340
forays.

03:54.340 --> 03:58.090
You often might want to check if a key exists.

04:00.580 --> 04:04.240
So for that we also will use a function.

04:04.240 --> 04:05.830
This is called array.

04:05.860 --> 04:07.540
Key exists.

04:08.440 --> 04:13.000
So let's see if the name exists inside the associative array.

04:13.720 --> 04:17.710
Well we can already see that that is true.

04:18.070 --> 04:26.320
And another thing is to check if a value exists in an array that's not limited to associative arrays,

04:26.320 --> 04:33.730
because we can check for values in both types of arrays, whether associative or indexed.

04:34.780 --> 04:37.600
So we've got just one element that's John.

04:37.600 --> 04:40.690
So I expect both to return.

04:40.690 --> 04:41.560
True.

04:41.950 --> 04:44.920
Yeah I've missed the array here.

04:44.920 --> 04:46.420
That would make sense.

04:46.420 --> 04:50.680
To specify in which array I would like to check for a value.

04:50.710 --> 04:52.900
As you see both has returned.

04:52.940 --> 04:53.720
True.

04:55.940 --> 04:59.180
So next up, something we've seen already.

04:59.180 --> 05:04.070
But let's just remind you of this in context of the arrays.

05:04.280 --> 05:08.480
So this would be converting arrays to strings.

05:08.510 --> 05:15.650
This also is something that you're gonna use a lot, especially when importing or exporting data.

05:15.680 --> 05:18.410
The functions called implode.

05:18.530 --> 05:27.530
And you can just add a separator that will be added between all the elements of your array.

05:27.560 --> 05:29.750
So we've got this fruits.

05:30.860 --> 05:36.530
And the other side of this function is explode.

05:39.050 --> 05:41.150
So I can use explode.

05:43.310 --> 05:45.260
Past the separator as well.

05:45.260 --> 05:54.690
And this would convert a string to an array where Just this characters will be used to separate the

05:54.690 --> 05:56.160
specific elements.

05:57.630 --> 06:04.710
Now let me vardump the fruit string and back to array.

06:06.990 --> 06:09.900
So first we've created this string from an array.

06:09.900 --> 06:15.180
And then from the string we've created we converted that back to an array.

06:18.330 --> 06:18.630
Okay.

06:18.630 --> 06:22.740
Now let's see how can we merge the arrays.

06:23.400 --> 06:27.060
So first there is an array merge function.

06:27.060 --> 06:33.510
And let's first call it on the indexed arrays those two sets of numbers.

06:37.410 --> 06:42.870
So when I call it we get an array of ten elements.

06:43.140 --> 06:51.640
So it seems that array merge is just adding elements from this array and elements from the second array

06:51.640 --> 06:54.670
without really giving it much thought.

06:55.450 --> 07:01.510
Okay, now let's see an array merge example with our associative array.

07:03.910 --> 07:05.860
So this would be associative array.

07:05.860 --> 07:12.010
And this should be just this data with the name country etc..

07:12.010 --> 07:14.320
And let me pass another array here.

07:14.440 --> 07:22.780
Now I know that this array contains a country key because I've added it and it contained USA.

07:22.810 --> 07:25.780
Let me change that to Germany.

07:26.620 --> 07:33.640
And first let me also output this array so we can see if anything has happened.

07:35.950 --> 07:38.110
So that's the original array.

07:38.830 --> 07:40.360
The country is USA.

07:40.360 --> 07:46.630
And in the resulting array from array merge the country is Germany.

07:46.660 --> 07:56.780
So in associative arrays, keys are taken into account, and if a key exists in the first array and

07:56.780 --> 08:00.350
in the second one, it will be overwritten.

08:00.350 --> 08:08.690
So the original keys from this first array will be overwritten if they are present in the second array.

08:10.340 --> 08:16.100
Okay, let me comment this out or remove it because there is also a union operator.

08:16.100 --> 08:18.650
That's just a plus sign.

08:21.890 --> 08:26.000
So let me call this without array merge.

08:28.760 --> 08:30.140
Just like this.

08:31.520 --> 08:39.290
And then let's also see what will happen in case of an associative array if I try to do the same.

08:44.360 --> 08:44.630
Okay.

08:44.630 --> 08:46.770
So here we've got the results.

08:47.430 --> 08:52.020
So with the plus operator, we've got this.

08:52.020 --> 08:56.040
This seems like we only have elements from the first array.

08:56.070 --> 08:57.660
I'm going to go back to this.

08:58.830 --> 09:08.220
And here with the associative array we can see that the country was not replaced and we still see USA.

09:08.250 --> 09:13.080
So let me explain how the union operator works on the arrays.

09:13.890 --> 09:18.720
So this union operator with arrays, it's based on keys.

09:18.720 --> 09:20.220
And the way it works.

09:20.220 --> 09:24.990
If there is a key in the original array it won't be modified.

09:24.990 --> 09:33.030
And the elements from the second array will only be added if they have a unique key.

09:33.330 --> 09:41.670
So since those both are indexed arrays, their keys are actually numbers from 0 to 4.

09:41.700 --> 09:49.210
Both have five elements, and since the keys are the same in both of them, we only see the elements

09:49.210 --> 09:56.560
from the first one, because the second array has the same indexes, the same numbers, thus nothing

09:56.560 --> 09:57.850
was modified.

09:58.540 --> 10:01.060
Now here we.

10:01.090 --> 10:03.430
We have added, let's call it.

10:03.460 --> 10:09.370
We have added an array which has a key that is already present in this original array.

10:09.400 --> 10:12.550
That's country was it was also.

10:12.730 --> 10:17.980
Well the resulting array did not have the country modified.

10:18.220 --> 10:26.770
Now if I would use a key that's not present in this first array instead.

10:27.460 --> 10:29.020
And let's see what will happen.

10:29.020 --> 10:33.190
Well then it would be added to this array.

10:33.310 --> 10:34.270
Okay.

10:34.270 --> 10:37.390
So let's have one more example.

10:37.870 --> 10:46.170
Let's just unpack both arrays into a new array using the spreading operator.

10:47.130 --> 10:48.750
So we do three dots.

10:48.780 --> 10:49.920
Three dots here.

10:49.920 --> 10:53.730
And there goes a comma, not a plus sign.

10:54.390 --> 10:56.940
Let's do the same right here.

10:59.670 --> 11:05.400
So there is not a plus sign but instead the spread operator.

11:07.410 --> 11:10.650
And let's see results here.

11:13.500 --> 11:17.700
So with the numbers we can see that this works like a right.

11:20.490 --> 11:25.830
So with the numbers we can see the result is like with the array merge function.

11:25.830 --> 11:29.520
Those elements were just added into the new array.

11:31.140 --> 11:33.780
And here with the associative array.

11:33.780 --> 11:39.000
Well let's change that back to country and run it again.

11:40.500 --> 11:43.390
So this also behaved like a ray merge.

11:43.420 --> 11:48.790
The country was replaced with the value from the second array.

11:51.250 --> 11:54.070
Okay, now let me comment out those results.

11:54.070 --> 11:57.970
And again let's rerun this.

11:57.970 --> 12:03.490
So here we've got an array with ten elements because we are just adding elements from first and the

12:03.490 --> 12:04.330
second array.

12:04.360 --> 12:08.470
Now if you'd like only have the unique ones.

12:09.190 --> 12:15.460
There is an array unique function to which we can pass the results of merge.

12:15.460 --> 12:20.290
And this now has only seven unique elements.

12:21.580 --> 12:32.740
Then if you just want a part of this, there is an array slice to which you pass, maybe set one, then

12:32.740 --> 12:34.450
you pass the starting index.

12:34.450 --> 12:37.120
Maybe that will be one and the amount of elements.

12:37.150 --> 12:38.920
Let's go with three.

12:39.890 --> 12:43.640
So you only get a slice of the array.

12:44.870 --> 12:54.530
And the final example is if you'd like to find something in an array, let's say you would like to get

12:54.530 --> 12:56.600
the index of an element.

12:57.230 --> 13:00.110
You can use an array search.

13:00.500 --> 13:05.420
So in the fruits array I like to search for banana.

13:06.110 --> 13:11.060
And I should get the index of this element in that array.

13:11.840 --> 13:15.800
So in case of fruits this is the second fruit vase.

13:15.830 --> 13:18.170
I get the index of one.

13:18.860 --> 13:19.250
Okay.

13:19.280 --> 13:21.800
So we've played a bit of arrays.

13:21.800 --> 13:30.950
Obviously there is always so much more information and so much more examples we can do, but I think

13:30.950 --> 13:38.270
this should really give you a solid foundations as the most important array functions and features were

13:38.270 --> 13:38.960
covered.
