Okay, so let's do a deeper dive on arrays.
Now we've been using arrays previously, but now in this video we are just gonna focus on them entirely.
So we've got a simple array here.
Um this is just indexed automatically by PHP starting with zero.
And then another example is an associative array where we choose the keys and assign values to them.
Now both arrays are or.
The values from both arrays are read the same way.
So you either use a number index like zero, or you use the key from the associative array.
This key needs to be defined there.
So in this case it's for example name.
And when I run this we see one because that's the first element in this array.
And we see John because this is under the name.
Next up to add elements to an array.
Let's start with the simple indexed array.
You just use this syntax square brackets next to the variable name next to the array.
And you can add anything.
Let's say we add six elements are always added at the end of this indexed array.
Now with associative array which is a little bit different you need to choose the key.
So we can add a country which can be USA.
So in PHP you can also have multi-dimensional arrays.
So we can just add an array inside an array like this matrix that I've created in here.
Though it's also possible that inside an indexed array like this one, you can add an associative array
and you access the elements using square brackets.
So this is getting the second array from matrix.
And from this array I'm also getting the second element which should display five.
So let me comment out those.
So we clearly see the result.
So I expect to see five on the screen.
And there it is.
Okay.
So next up let's see some functions that we can use with arrays.
So we've got this fruits array.
We can count them using the count function.
So it should display three.
And then we can sort arrays.
So with sorting, you need to know that sorting works in place, which means the original array would
be modified.
So let's vardump this and the sort function is doing an ascending sort.
So to have a descending sort you do a sort.
Again this also modifies this variable.
It doesn't return a new one.
Okay.
So let's also vardump this.
I'm going to comment out this line and let's run this script.
So the count of elements is three.
Then the elements when sorted in ascending order we've got this fruits and then sorted in descending
order.
That's the order.
Okay.
Now let's do something with associative arrays.
First let me dump the associative array.
Um, what's the name of it?
Okay, that's just associative array.
Now you can also sort the associative arrays, but here we can sort the elements by key or by value.
Okay.
Um a sort will sort by value.
Okay.
Let's vardump.
This also sorts the array in place.
So it is modifying the original variable.
Then we've got k sort.
This as you can guess is sorting by keys.
Let's also vardump that one.
Okay.
Let's run it.
So that's the original array in the order that we have defined that we've also added a country in at
one place.
Then we sorted using a sort.
So this was sorting by value.
That's the result.
And this here is sorting by key.
As you can see keys seem to be sorted alphabetically.
Okay.
So next up let's create some numbers.
So the range function well this one is useful.
You can just generate an array of numbers from 1 to 5.
That's obviously an example.
You can choose whatever numbers you want.
So let's vardump this one.
And also I'd like to make sure that we don't have the screen too cluttered.
So I'm going to comment those out.
So let's run it.
This is our array of numbers.
As you can see, it is indexed starting from zero.
Then let's do something with this array so we can use some functions.
So I think we've already had this example of squaring the numbers.
But let's have it again.
So we have array map function.
Now this will return a new array and we can pass a callback to it.
So we are passing an arrow function that will do something to every element from the array.
So we square the number.
And we also need to pass the array to this function.
Okay.
Now let's vardump the results.
And let's run it.
So that's the original one and that's the results array.
All right.
Now another one is an array filter.
So let's create an array of even numbers and use array filter to only get the even numbers from either
numbers or squared array.
I'm going to go with numbers.
Now as you can see, PHP is not really consistent with the order of arguments.
So with array map the array came second with array filter it comes first.
This is just how it is.
Nothing we can do about this.
Okay.
Let's have an arrow function here.
This is function n and I think we've seen that before.
So we use the modulo operator and we get the remainder of dividing by two.
If it happens to be zero, this is an even number.
Never forget the semicolon.
And why won't we vardump this?
To see the results and all those functions.
Array.
Filter.
Array map.
They will produce new arrays, so they just don't modify the original array that you've passed to it.
Typically this only happens with sorting.
So let's run this one.
And here we have it from the original numbers array.
Only two and four are even numbers.
Okay.
Next up let's see an array reduce function.
So this might be a tough one.
So let's make sure you focus on this one.
So we call array reduce.
Now the goal of array reduce is to reduce the array to one single value.
So in this case first argument would be the array.
Next up would be a function.
I'm going to add it in a second.
And there is also a third argument which is the initial value.
Since we want to reduce that the array to one single value, we also start with an initial value, which
in this case is zero, because we want to calculate the sum of numbers.
So we start with zero.
And now it's important how the callback function looks like.
Let me move every single argument into a separate line.
So your function will receive two arguments.
The first one is called carry and the second one is the element from the array.
Now you don't have to call this argument carry.
It's up to you.
But this is how this is called in the array reduce function, which is not unique to PHP.
And the way it works is we go over every single number from the array.
So if we take a look in here we've got five of those.
And you should return a single value from this callback.
So in our case if we want to calculate the sum, we return the value that was carried from the previous
run or the initial value of that was passed to array reduce.
So this one, when this callback is called for the first time plus n okay.
So with the first call we've got an initial value.
So carry would be zero.
And first element is one.
So we return one from it.
But on the second element of this number's array Carrie is now one.
So we add 1 to 2 and you see where I'm going.
So let's vardump this.
And this also doesn't modify the original array.
And the sum is 15.
Okay.
So now let's see something new.
The array unpacking.
I think this was only introduced in PHP 7.4.
So I'm going to define a more numbers array.
So array is always defined by square brackets.
I'm going to add one value to it.
And then I'm going to unpack the numbers array.
And also I will add something at the end.
So the way it works does a three dot operator before an array doesn't have to be variable.
You can just add an array directly in here like that.
So this operator in this case will just move all the elements from this numbers array into this more
numbers array.
Let's see this in practice.
And I think everything would be clear what's happening in here.
Let's run it right now.
And this is our new array.
So the element that I've added everything that was unpacked from numbers array and this one that we
have added at the end.
Okay.
So this is array unpacking.
Okay.
So next up let's see array destructuring.
So remember our fruits array.
Let's use it.
And using this assignment I want to get the first and second element of the arrays and assign them to
a variable called first.
And a variable called Second.
So let's see what's the result of this.
So this should be some fruits but I'm not sure which ones.
Okay.
So the value of first variable is apple.
The value of second is banana.
So they seem to be sorted.
I can also skip elements.
So uh sorry not here I can skip elements in here.
So actually the variable second would contain the third element.
So definitely not banana.
Now this is orange okay.
Anyway this is possible.
Okay guys.
So I was thinking about going through arrays in one go.
But I think this would be too long.
So let's have a short break and go back to more arrays in the next video.