Okay.
Now let's define those two arrays.
Those will be just a set of numbers.
And we're going to calculate the intersection of those two arrays.
So the common elements and then the differences between those two arrays.
First to get the intersection we call array intersect.
And we pass those two variables.
Now with intersection the order you pass the arrays does not matter because the common elements set
would be always the same.
Now there is also an array div function which is used to find the differences.
So if I pass set one and set two, this means that I would like to see all the elements that are present
in set one, but are not present in set two if I invert the order, this will actually give me a different
result, because here I'm interested in elements that are in the set two but are not in set one, and
those are a little bit different.
They don't overlap.
So first if we run that we can see that for the intersection we've got the same results.
This makes sense.
The common elements would always be the same but with the different elements.
First we've got one and two.
These are not present in this set.
And if we invert the order of parameters we've got six and seven.
So this makes sense that those numbers are not present in the first set.
Okay.
Now keys and values.
So let's go back to this associative array we've defined at the top.
And in PHP, you've got a way to just get all the keys and all the values of a specific associative
array, which would produce a new array.
So that's pretty simple.
You do it using functions.
First we've got array keys.
This will just give us all the keys.
And then we've got values which we retrieve using array values function.
Now this is actually quite useful.
So let me dump both.
So here is the result of that.
And those two functions are actually pretty handy.
For example with the keys I can use that to label the table headers.
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
the first letter.
So I've got array keys here.
Maybe let me show you another example.
So I can do array map.
Then with every key I'm going to do you see first key and pass the array of keys to it.
And let me run that again.
And now I've got nicely formatted labels or table headers.
So next up we've associative arrays.
forays.
You often might want to check if a key exists.
So for that we also will use a function.
This is called array.
Key exists.
So let's see if the name exists inside the associative array.
Well we can already see that that is true.
And another thing is to check if a value exists in an array that's not limited to associative arrays,
because we can check for values in both types of arrays, whether associative or indexed.
So we've got just one element that's John.
So I expect both to return.
True.
Yeah I've missed the array here.
That would make sense.
To specify in which array I would like to check for a value.
As you see both has returned.
True.
So next up, something we've seen already.
But let's just remind you of this in context of the arrays.
So this would be converting arrays to strings.
This also is something that you're gonna use a lot, especially when importing or exporting data.
The functions called implode.
And you can just add a separator that will be added between all the elements of your array.
So we've got this fruits.
And the other side of this function is explode.
So I can use explode.
Past the separator as well.
And this would convert a string to an array where Just this characters will be used to separate the
specific elements.
Now let me vardump the fruit string and back to array.
So first we've created this string from an array.
And then from the string we've created we converted that back to an array.
Okay.
Now let's see how can we merge the arrays.
So first there is an array merge function.
And let's first call it on the indexed arrays those two sets of numbers.
So when I call it we get an array of ten elements.
So it seems that array merge is just adding elements from this array and elements from the second array
without really giving it much thought.
Okay, now let's see an array merge example with our associative array.
So this would be associative array.
And this should be just this data with the name country etc..
And let me pass another array here.
Now I know that this array contains a country key because I've added it and it contained USA.
Let me change that to Germany.
And first let me also output this array so we can see if anything has happened.
So that's the original array.
The country is USA.
And in the resulting array from array merge the country is Germany.
So in associative arrays, keys are taken into account, and if a key exists in the first array and
in the second one, it will be overwritten.
So the original keys from this first array will be overwritten if they are present in the second array.
Okay, let me comment this out or remove it because there is also a union operator.
That's just a plus sign.
So let me call this without array merge.
Just like this.
And then let's also see what will happen in case of an associative array if I try to do the same.
Okay.
So here we've got the results.
So with the plus operator, we've got this.
This seems like we only have elements from the first array.
I'm going to go back to this.
And here with the associative array we can see that the country was not replaced and we still see USA.
So let me explain how the union operator works on the arrays.
So this union operator with arrays, it's based on keys.
And the way it works.
If there is a key in the original array it won't be modified.
And the elements from the second array will only be added if they have a unique key.
So since those both are indexed arrays, their keys are actually numbers from 0 to 4.
Both have five elements, and since the keys are the same in both of them, we only see the elements
from the first one, because the second array has the same indexes, the same numbers, thus nothing
was modified.
Now here we.
We have added, let's call it.
We have added an array which has a key that is already present in this original array.
That's country was it was also.
Well the resulting array did not have the country modified.
Now if I would use a key that's not present in this first array instead.
And let's see what will happen.
Well then it would be added to this array.
Okay.
So let's have one more example.
Let's just unpack both arrays into a new array using the spreading operator.
So we do three dots.
Three dots here.
And there goes a comma, not a plus sign.
Let's do the same right here.
So there is not a plus sign but instead the spread operator.
And let's see results here.
So with the numbers we can see that this works like a right.
So with the numbers we can see the result is like with the array merge function.
Those elements were just added into the new array.
And here with the associative array.
Well let's change that back to country and run it again.
So this also behaved like a ray merge.
The country was replaced with the value from the second array.
Okay, now let me comment out those results.
And again let's rerun this.
So here we've got an array with ten elements because we are just adding elements from first and the
second array.
Now if you'd like only have the unique ones.
There is an array unique function to which we can pass the results of merge.
And this now has only seven unique elements.
Then if you just want a part of this, there is an array slice to which you pass, maybe set one, then
you pass the starting index.
Maybe that will be one and the amount of elements.
Let's go with three.
So you only get a slice of the array.
And the final example is if you'd like to find something in an array, let's say you would like to get
the index of an element.
You can use an array search.
So in the fruits array I like to search for banana.
And I should get the index of this element in that array.
So in case of fruits this is the second fruit vase.
I get the index of one.
Okay.
So we've played a bit of arrays.
Obviously there is always so much more information and so much more examples we can do, but I think
this should really give you a solid foundations as the most important array functions and features were
covered.