‫Okay.
‫So in the last section we went over variables and data types.
‫Now we're going to look at arrays.
‫And then we're going to move to loops and iteration and also looping through arrays.
‫So an array is a special type of variable that can hold multiple values.
‫And they're great for storing lists of data.
‫So I just have some points key points that I want to go over when it comes to arrays in PHP.
‫So you can think of them as versatile containers that can hold various pieces of information, numbers,
‫words, or even different types of data.
‫And it's like a numbered list where each item has its place, so you can always find them in a specific
‫order.
‫You can also mix and match data types.
‫They're very flexible, so you could have in the same array a string, a number, a boolean.
‫There are other languages where your arrays have to all be the same data type, but that's not the case
‫in PHP.
‫So easy access.
‫You can access each piece of data using a special number or name that identifies its location in the
‫array.
‫This is called an index, and this makes it easy to retrieve and work with values in that array.
‫They're also used for useful for grouping.
‫You can group related data together like a list of names, a set of numbers, or key value pairs for
‫storing information like names and ages, and so on.
‫Also, arrays have dynamic sizing, and some languages you have to actually you have to statically state
‫how long that array can be or how many items can be in that array.
‫With PHP, they can grow and shrink as needed.
‫All right.
‫Now, when creating an array, there's actually two ways to do it.
‫So the first method is to use the array function.
‫So you can create a variable such as names.
‫Set it to array.
‫And then in parentheses pass in your values.
‫In this case we have three strings three names.
‫The second method is what you'll most likely see more often, and this is what we'll use in the course
‫where we don't use the array function, we just use brackets.
‫So you can set names to brackets and put the values inside there.
‫Now when you want to access a specific value, a single value.
‫And I'll show you this in the code in a minute.
‫But you can do that with the index.
‫So in this case we're saying echo names.
‫And then in brackets we have a one.
‫And that's going to give us Jack.
‫Now if you're brand new to programming you might be saying well isn't that too.
‫No.
‫Because arrays are zero based in every language that, that I've ever used.
‫Arrays are zero based.
‫So zero would be John, one would be Jack, two would be Jill, and so on.
‫Okay, so now let's jump into some code and create and work with a couple arrays.
‫So I just have an empty file here.
‫Let's add our PHP tags.
‫And I'm just going to create the names array that we saw on the slide.
‫So I'll do it the first way which is using the array function.
‫And I don't usually use this.
‫But of course I want you to know both ways.
‫So we'll say John, Jack and Jill.
‫And then let's do another array of numbers.
‫So this will be ints.
‫And I'm going to use the second method which is just using brackets.
‫So we can just go ahead and do that.
‫So we have an array of strings and an array of numbers.
‫Now there's going to be times where you're going to want to print out all the values of the array.
‫And I mean for you, for your, you know, for your benefit, for debugging, not for the the users.
‫When you show array values to users, you're most likely going to loop through the array and then format
‫the output to what you want.
‫But for debugging, you can use Vardump as we were using with regular variables.
‫So let's say vardump names.
‫And if we take a look at that you'll see we get the array.
‫We just.
‫Make that a little bigger.
‫So we have the array and the length of the array.
‫And in here we'll see the index for the first one is zero.
‫And it points to the string with the length of four of John, one Jack two Jill and so on.
‫Now this right here, I believe this is part of a Chrome extension that I'm using.
‫If you have HTML on the page, that won't show.
‫So for instance, if I come down here and let's say.
‫Let's go like that.
‫I don't believe that.
‫Yeah, so that won't show.
‫But this will.
‫So what I often do is.
‫Is format the var dump with pre tags.
‫So you'll see if I echo here.
‫HTML pre tags.
‫So pre.
‫So that's the opening.
‫And then down here we'll have the closing.
‫If I do that then it'll format it nice and neat okay.
‫And I could have HTML down here as well.
‫So if I do that, it's still formats.
‫Nice and neat and you might even want to kill everything after that.
‫So in that case you could use the die function.
‫If I do that, it's going to not show any HTML or anything that comes after that.
‫And what I'll typically do is put this into a function called like dump and die or inspect or something
‫like that, and we will do that in our project.
‫In fact, let's do that.
‫Now.
‫I know we haven't gone over functions yet, but this will help us just to see what what's in the arrays.
‫So let's say inspect.
‫And you can call it whatever you want.
‫And I'm going to call I'm going to pass in a value.
‫And then what we'll do is grab this.
‫And we'll put that in here and just replace names.
‫With value.
‫You just format this?
‫I don't know why it didn't.
‫Automatically.
‫I'm actually going to take the die out, because if we keep the die in, we can only use it on one array.
‫We might want to inspect more than one at a time.
‫And then down here we'll say inspect.
‫And let's pass in names.
‫Okay.
‫So we get the same thing.
‫And then I could also do inspect say numbers.
‫If I do that now, we see that as well.
‫If I had die in here, then it's only going to show the first one because it's not going to do it kills
‫anything after this.
‫So I'm going to get rid of that.
‫And you can see this is an array has six items in it.
‫The indexes are 0 to 5.
‫All right.
‫So I'll just comment that out.
‫Now there is another function that you can use called print R.
‫I don't use this very much, but it is another way to show all of the values.
‫So if we say print our names like that.
‫There we go.
‫And I believe this is formatted like this because of the Json format or extension that I have.
‫Uh, yeah, I believe that's why.
‫Or it might be the source code extension.
‫I don't know, yours may look a little different, but I'll usually use vardump.
‫I'll usually wrap it in a function like this, like inspect.
‫All right.
‫Now, as far as as far as seeing single values, specific values from an array, we can do that by using
‫the index.
‫And we can echo that out because it's just a single value.
‫If I try to echo out an entire array, then I'm going to get an error or a warning.
‫It says array to string conversion.
‫So echo is used on strings, but I'm trying to do it with an array.
‫So it's giving me a warning.
‫So if I wanted to access a single value I could use the index like let's say names two.
‫And that gives me Jill and remember zero base.
‫So this is 012.
‫Okay.
‫If I want to get, let's say for the numbers, whatever is in the position of three.
‫Reload and we get four.
‫Okay.
‫And the reason we get four again, it's zero based one.
‫The number one is in the zero position, two is in the one position three is in the the two position
‫and four is in the three position.
‫So now that we looked at accessing the entire array or specific values, let's look at adding changing
‫values and removing values.
‫So I'm going to just comment out this these echoes here.
‫And let's say we want to add element to array an element item value.
‫These terms are used interchangeably.
‫Now there's a method.
‫There's many methods that we can use to add and remove values.
‫But we're not we're not there yet.
‫So I'm going to show you kind of the lowest level way of doing it.
‫So if I want to add a value to the end of let's say numbers.
‫So I could come up here and count.
‫So this is zero, one, two, three, four and five.
‫Right.
‫So the next one would be six.
‫So I could say number six.
‫And let's set that to oops we don't want a string.
‫We want that to be an integer.
‫So 100.
‫And then let's do inspect.
‫Numbers.
‫And if we do that, we can see that now we added 100 on to the end here and it has the index of six.
‫However, you might not always know exactly how many items are in the array.
‫So what you can do is just have an just set empty brackets.
‫Right.
‫So I'll do that and then I'll do another set that to 101.
‫If we reload now, you can see we still have 100 in the sixth position, and then we have 101 in the
‫seven.
‫And it'll just continue to increment.
‫So that's one way to add to the end of an array.
‫But like I said there are methods that we can use to do this.
‫And that's the more common way.
‫And when we'll get to that soon.
‫Now as far as removing items now let's let's look at changing first or updating.
‫So let's say I want to change.
‫Let's see the three position is the number four.
‫Let's say I want to change that.
‫So I'll come down here and I'll say numbers oops numbers at the three position.
‫And I want to set that to let's say 200.
‫And so if I come back over here, reload.
‫Now, you can see that the three position has the value of 200.
‫Now to delete or remove an item we can use unset.
‫So let's come down here and let's say unset and pass in numbers.
‫And we'll do three the 200 okay.
‫So I'll save that reload.
‫And now you'll see that's gone.
‫Now one thing to to realize is that the index is also gone.
‫So now it's 012 and three is now gone.
‫Now in many cases, or I'd probably say most cases, this doesn't really matter if because in a lot
‫of cases you're not specifically using the index.
‫But if you want to re index it you can use the array values function.
‫So I'm going to go under the unset and I'm going to reset or reassign names equal to array underscore
‫values.
‫And then I'm going to pass in I'm sorry not names.
‫We're doing numbers.
‫So numbers and we want to set.
‫Numbers.
‫Okay.
‫So if I do that and then I reload.
‫Now it's re indexed.
‫So it's 01234 and so on.
‫Okay.
‫So these these types of functions, there's a lot of them to just work with and manipulate arrays.
‫So that's what we're going to do in the next video is look at a lot of the common functions that we
‫use with arrays.