‫Okay.
‫So now we're going to look at functions that we can use to manipulate and work with arrays.
‫And I just have my my template here with my output which is set to null by default.
‫Now it's important to know that some of these functions will do something and then return a new array.
‫So you'll have an array.
‫You'll use a function and it will return a brand new array with whatever the changes that function did.
‫Others will not return anything.
‫They'll they'll manipulate the existing array.
‫And I'll point those out and show you the difference when we get to it.
‫So let's create a couple of arrays to work with.
‫So I'm going to create an array of IDs.
‫And from now on I'm just using the brackets syntax.
‫I just wanted to show you the other you know the array method as well.
‫But we'll create some numbers here.
‫So IDs numbers whatever.
‫So 45 and let's do 67.
‫And then we'll create a string array of users.
‫So we'll say user one, another string user two and another string user three.
‫And I didn't show you this in the last video, but you can mix and match like I could put a number in
‫here.
‫I could put a boolean, I could put null.
‫So you can put different data types in arrays.
‫So now we have two arrays.
‫The first, the first function that I want to look at is count which will return the number of items
‫in the array.
‫So let's set output and I'm going to set that to count.
‫And let's pass in IDs okay.
‫And then if I reload the page here we get five.
‫So there's five numbers in here.
‫If I were to add another one.
‫Then we get six.
‫So that's an easy way to get the length.
‫Now the next batch of functions I'm going to show you they don't actually return anything that I want
‫to put in this output.
‫They they directly manipulate the array.
‫So what I want to do is in addition to showing whatever is in the output variable, I also want to show
‫the arrays themselves.
‫So let's go under this paragraph here.
‫And I'm going to have an H to.
‫Let's just add text.
‫We'll do text dash XL for the size and let's say font dash semi bold and margin on the y axis for so
‫MY4.
‫And this is going to be the IDs.
‫Array.
‫Okay.
‫And under that h two I'm going to have a paragraph and a pre tag.
‫And I'm going to use print R here.
‫So let's say PHP and then print underscore R because that will show an array.
‫And I want to show the IDs array.
‫So if we save that and reload we should see that.
‫Now I also want to show the users array.
‫So I'll just copy that H two and this paragraph here.
‫Actually I don't know why there's two ending paragraph tags that shouldn't be there.
‫So we just want.
‫Yeah, we just want this.
‫Okay.
‫And then we'll change that to users array.
‫And then we want to print our.
‫Users.
‫So now reload.
‫And now we can see both.
‫Okay.
‫So let's come up here and I'm just going to put some comments.
‫So this is the count function.
‫Next I want to show you the sort function.
‫So again sort doesn't return the array.
‫This is going to just manipulate the existing array.
‫So let's run sort on the IDs array.
‫And right now, if you take a look.
‫I didn't reload it yet.
‫You see, zero ten.
‫The one position is 22.
‫The two position is 15.
‫If I reload now, zero is ten, one is 15, two is 22.
‫So it's in order now.
‫We were able to sort it.
‫So let's run sort on users as well.
‫So users are is a bunch of strings reload and let me just move this around.
‫So if I say user one here, user two reload, it's still going to be user one at the beginning and user
‫two and then three because I sorted them alphabetically.
‫Okay.
‫We also have our sort which does the same thing but in reverse.
‫So if we say our sort.
‫IDs and our sort.
‫Users.
‫If we do that.
‫Reload now, they're in the reverse order.
‫So the next one is, is a function that you're going to use quite a bit.
‫And that's array push right.
‫So this will add a value on to the end of the array.
‫So let's say array underscore push.
‫And if you're coming from JavaScript it's similar to the array method where you would say like users
‫dot push and you could push on a user similar to that.
‫But instead of being a method that's actually on the array, it's a function that we call and pass the
‫pass the array in.
‫So let's say array push.
‫It's going to take in two things.
‫One is going to be the id.
‫The array that we want to push on to.
‫Second is the value.
‫So I'm going to say 100.
‫So if I reload you'll see that 100 has now been added to the end of the array.
‫And if I wanted to add a user I could do array push on users and let's add a user called user for.
‫Reload.
‫And now that gets added.
‫So notice that this is no longer in reverse order.
‫Right?
‫So it's 3214.
‫That's because I called the sort before I pushed it on to the end.
‫So it's going to push on to the end no matter what, no matter what the order is at that time.
‫So keep that in mind.
‫The next is pop which will pop the element off the end of the array.
‫So pop.
‫And I should say it's a Ray pop, not pop.
‫Getting my JavaScript methods mixed up.
‫So array underscore pop.
‫And that's going to take in just the array.
‫So let's say IDs and it just takes the last one off I'll do it to users as well.
‫So it should take off the ones I just added 100 and user four.
‫So if I save and reload you can see that now those are removed from the arrays.
‫Now if you want to shift an element off the beginning of the array.
‫So if you want to take off the first element you can use array shift.
‫So array underscore shift and let's say array underscore shift.
‫And that's going to take in just the array name as well.
‫So we'll say IDs.
‫And then we'll also do it for users.
‫So if you look at it right now we have 67 is the first and user three is the first.
‫If I reload now those are gone.
‫Now you can also add to the to the beginning, or prepend an element to the beginning with Unshift.
‫Or array.
‫Unshift.
‫So let's say array underscore unshift that's going to take in two arguments.
‫First is going to be the array itself.
‫So IDs and let's add 100.
‫And then let's do for users we'll add user for or we'll do user three.
‫Since we already got rid of user for user three we'll re add it here to the beginning.
‫So let's save that reload.
‫And now user three is put at the beginning and 100 is put at the beginning here.
‫Now the next function that I want to show you is called array slice or array underscore slice.
‫And this isn't one that just directly manipulates the array like these ones do.
‫This will actually return part of the array as a new array.
‫Okay, so instead of just calling it I'm going to put it into a variable.
‫And I'm going to I'll call it IDs to.
‫Okay.
‫And this is going to be an array.
‫Now what this takes in let's say array slice.
‫So this will take in the array that I want to slice from.
‫So let's say IDs.
‫And then it takes in the index where I want to start the slice.
‫So let's say I want to start it at 33 which is two right.
‫So I'll pass in two.
‫And then the next one is the length I want to go from there.
‫So let's say I want to go to 15.
‫So I want to go 33 2215.
‫So that's a length of three.
‫So I'll pass in three.
‫And then I can't just echo IDs IDs too because it is an array.
‫So I'm just going to do a var dump here temporarily.
‫And we'll pass in IDs too.
‫And then let's see what that gives us.
‫So we have an array with three items.
‫First is 33 okay.
‫Second is 22 and the third is 15.
‫So hopefully that makes sense.
‫And this is now a completely different array than IDs.
‫It doesn't affect IDs at all.
‫So I'm just going to comment that VAR dump out because I don't want that displayed anymore.
‫But that's how array slice works.
‫Now there's also a function called array splice.
‫And this can get a little confusing.
‫So what array splice does.
‫Let me just put this here.
‫So array underscore splice.
‫So this will return a portion of the array and replace it with something else.
‫So what I'm going to do is call array.
‫And this one doesn't actually you know we're not returning something that we want to put into a variable.
‫We're manipulating the array directly just like the other ones above.
‫Above this.
‫So let's say array splice.
‫And I'm going to pass in the the IDs array.
‫And.
‫If we look at the function signature here, the first argument is the array.
‫Second is the offset.
‫The third is the length.
‫Okay, so that's what I want to pass in as the offset and the length.
‫So where do I want to start.
‫I'll say one and a length of one.
‫So basically I'm going to just replace the position of one which is the value of 45.
‫And I'm going to replace it with.
‫Whatever I put as the fourth parameter, which I'll put, I'll just make it a string and I'll say new
‫ID, just so it's clear which one I replaced.
‫So let's save that and reload.
‫And now you'll see that the index of one has been replaced with a string of new ID.
‫Okay, we'll do the same thing.
‫With users.
‫Okay.
‫So let's say users and let's get the first one.
‫So the zero index I'm going to change that to zero.
‫And we'll just do the first.
‫So we'll keep this at one.
‫And then let's change this to let's say new user.
‫So if I reload now, that's changed to new user.
‫Okay.
‫So now let's look at some some functions that return certain things.
‫So I want to look at array some.
‫So this is similar to like if you're coming from the JavaScript world, it's similar to the reduce method.
‫It's going to just add everything in the array.
‫So let's actually put this in the output variable that we're echoing down below.
‫So I'm going to have a string.
‫We'll say sum of IDs.
‫Colon space.
‫And then I'm going to concatenate onto that array underscore sum.
‫And we're going to pass in IDs.
‫So now if I reload we get some of IDs is 180.
‫So basically it took everything in here and added it.
‫So the next one is array search.
‫So array underscore search.
‫And as you can probably guess this will search the array for a given value.
‫So I'm going to set output.
‫And say user.
‫Let's see.
‫User two is at index because that's what it will return is the index.
‫Is at index colon and then oops, forgot my quote.
‫So user two is at index.
‫And then we'll concatenate onto that array underscore search.
‫And I'm going to search for user to.
‫From the users.
‫Users.
‫Array.
‫All right.
‫So let's see what that gives us.
‫So user two is at the index one which if we look down here it is.
‫So it takes in the value and the array and it will return the index of that value.
‫Next is in underscore array, not ins in array.
‫And what that will do is it will check if a value exists in an array, which is obviously very helpful.
‫So let's say output set that to a string.
‫We'll say user to actually let's.
‫Let's say user three.
‫Exists.
‫And then we'll concatenate in underscore array.
‫And let's check for user three, which you can see doesn't exist, but we want to check that in the
‫users array.
‫So go ahead and reload and we get nothing because it's false.
‫Now let's say we add it.
‫We'll go right here and we'll say array underscore push.
‫And we're going to push on to users.
‫The value of user three.
‫So it shows down here.
‫And then it also shows as a one right here.
‫When it's output to the browser, it's going to be a one.
‫But it's actually true.
‫So if I were to take this.
‫Right here.
‫And let's do a var dump so we can see the type.
‫Right?
‫So if I do that reload, you can see that it's actually a boolean.
‫But that's this isn't going to if you try to echo it out to the browser, it's not going to show this.
‫It's going to show a one.
‫All right.
‫We'll just get rid of that VAR dump right there.
‫And then there's two other functions that I want to show you.
‫One is called explode and one is called implode.
‫And these are used to turn to turn either an array into a string or a string into an array.
‫And you'll actually find yourself using these quite a bit.
‫So let's first look at explode, which will turn a string into an array.
‫And at first you might say, well, why would I want to do that?
‫But let's take a look at a pretty practical example.
‫So let's say we have some tags right.
‫So we'll do comma separated values.
‫We'll say tech code.
‫Um, I don't know.
‫Programming.
‫Whatever.
‫So we have this comma separated value list of tags.
‫And we want this to be turned into an array with these values.
‫Right.
‫Actually let's not put a space in between.
‫So just just commas.
‫All right.
‫And then I'm going to create another variable let's call it tags array.
‫And we're going to set that to explode.
‫Okay.
‫So explode will turn a string to an array.
‫And what we want to pass in here, first of all, is the delimiter by which we want to split each of
‫the array items.
‫And that's going to be a comma.
‫So basically I want everything in between the commas to be an array item.
‫So that's the first the first argument.
‫The second is the array itself which is tags I'm sorry the string which is tags.
‫And then let's.
‫So since this is an array what we'll do is just do a var dump.
‫And let's say tags array.
‫All right.
‫So we'll go ahead and reload that.
‫And now we have an array with three elements.
‫The first is a string of of tech.
‫Second is a string of code.
‫Third is a string of programming okay.
‫So I mean you can see why this would be useful if you have, let's say a web form where people are typing
‫in tags for their posts like this, and you want to save it as an array, then you can use explode.
‫And at the same time, you might have an array where you want to turn that to a string of comma separated
‫values.
‫So let's do that.
‫Let's comment out the var dump let's say implode.
‫And what I want to do now is for the users I have.
‫I want those two to turn.
‫I want to turn this into a string, a comma separated list.
‫So let's actually put it in output, since we can echo out because it will be a string and then implode.
‫And what that's going to take in again is the delimiter that we want to use this time.
‫It's not how we want to separate them, but what we want to use in our string.
‫Let's use a comma.
‫Okay.
‫You could do a space comma or however you want to separate them.
‫And then the array name which is going to be users.
‫So we're setting that to the output variable.
‫So let's reload.
‫Uh, I'm sorry we have to do this.
‫If to comment that out because this is overriding this.
‫Actually, I'm not going to comment it out because this should be up here.
‫Yeah, we'll put that right there.
‫All right, so now if I reload in our output you can see new user comma user to user one.
‫User three.
‫You might want a space as well.
‫So you could add here in the implode first argument you could do a comma space reload.
‫And now you have a comma space in between each one.
‫So we took the array this array and we turned it into a comma separated list.
‫All right.
‫Now there's a lot more array functions.
‫I'd be here all day.
‫I could probably create a whole course on the array functions, but these are a lot of the common ones.
‫And we will get into some others as we move along, especially in the project.
‫So I'm going to paste this link here to the docs where you can find more array functions if you want
‫to take a look.
‫So in the next video we're going to look at something that's really, really important in PHP.
‫And that's associative arrays.