PHP Arrays and Manipulation

Hey guys, welcome back in this video we will be looking at arrays in PHP. Now up until this point, we have been looking at variables, which we defined as storage spaces for values or some form of content such that we didn’t have to retype this content over and over again, no, a variable is more like a singular space, meaning that if you change the value of the variable, one time, it can only store one value at a time, so to speak. Now, an array is like an extension of a variable where we can store multiple values inside of one variable like structure. Now to get things started off, as usual, we create a new file. I read dot php, as we already have the link there. And I’m just going to go ahead and create that file. And then we go ahead and spin up our html5 templates. Then we go ahead and change the title to what we know is the standard PHP primer dash hurries. And then we put in our h1 tags for our arrays, notification, and then we get our hands dirty. Now I did start off this lesson by saying, sorry, I did start off by saying that we have a new construct called under called an array, which is similar to a variable now we’ve seen variables where we declared maybe num, and then we could say is equal to three. But what if you needed to store more than one numbers, and you didn’t want to have a variable per number because if we wanted 10 numbers, and we would have to probably start seeing on one num two, number three, number four, and storing them all in each variable, but then an array allows us to use one variable and store many values. So I’m going to declare, so I’m going to just comment this and see this is a variable. And then underneath that, I’m going to comment NC and this is an array. And then I’m going to declare an array by seeing numbers. So it looks just like the binary declaration, our variable declaration initially, but then we say equals our ri, and then open parentheses and then inside these parentheses, we add as many values as we will want to. So I did see numbers, so I’m going to add each number and it is comma separated. So I’m going to add numbers one through 10. And so what we have here is an array of numbers one through 10. All right, so we can have an array of values and this is all We call it a homogenous array, meaning only one type is being stored now in most programming languages, that is the norm, but in PHP they actually allow us to store more or more than one types of values are types of data. So we know we have string for text, we have numbers, and we have well decimal places. In other programming languages are more strict. But in PHP, you can store almost any, you know, combination, if you want. So in this case, we’re only storing numerals. So I’m calling it a homogenous and homogenous array. So know that we have established century What if we only wanted to print one off the numbers at a time. Now an array is numbered from zero to n, n being the number of values in that array. so in this situation, the subscripts as we call it would be zero for the number one, one for the number two, etc. So at this point is always minus one. But the fact is that regardless of the data type, or whatever it is, you’re storing in sidari, the first one is always zero. And the last one is always n minus one, where n is the total number of values in said array. So if I wanted to print any one of these values, just the value of not all of the values, I could Echo, and I would call my array, which is called numbers. So it’s the same construct as printing a variable, except I have multiple values. So I want to specify the value I want, then I have to know which number it is in the array. So if I wanted to print the number six, since we’re starting from zero, it would be 01234 Five. So if I print the subscript five, and I get the subscript by using my square brackets next to the name of my array, so my array is numbers. So I put echo numbers, and then open and close square brackets and inside of these square brackets, I put the number of the ri that I want to print. Right, so that’s a subscript, that’s the location, the position, however you need to term it, it just means that I can find the value I’m interested in at this position in the array. And remember, once again, it starts at zero and ends at n minus one. So I want to print the number six, it would be 123456 minus one because this was really zero. All right. So if I do that, and then I browse to the page then we see Where I am printing the value six, which is indeed in the fifth position of my array because I started counting at zero. Now I can repeat this with any other subscript that I want. So I’m just going to echo a lot, another lion, and this time, I’m going to put it in a p tag. So I’m going to just put the variable or the reference to the RA and subscript inside of quotation marks so that I can get that interpolation. And I’m going to put it in a p tag. And I’m going to change this subscript i want to print the last value. So like I said, it’s n minus one, so I have 10 items. So 10 being an n minus one would be nine. And then if I refresh this, I see I have six being printed first and then the number 10 being printed after and other cool things you can do with an array. To find the size. So let’s just say I wanted to know how many elements are in my array, and I’m going to put in some other random values. So I’m just expanding this array. So that wasn’t very wise of me. So I’m just expanded my fingers hit a few keys that they shouldn’t have me hit a comma twice. And I’m going to get rid of that extra comma. And so at this point, I didn’t count how many key tops I made. I just want to know how many values are in this array. So I’m just going to echo and I’m going to use double quotation marks, I can just use my interpolation and then I’m going to see p tag. Ari. numbers is size and I can Just see something like cones. Okay, well, this won’t actually work. So I just realized this won’t work. Because cones is a function and interpolation really only works with actual variables. So I’m going to have to do this a bit differently. So I’m going to close my p tag, just the same. And then I’m going to take this out, because this is the function that gets the size of the array. So I’m just going to declare a variable, because I can declare these whenever I want. And I’m going to say size is equal to, and then I’m calling the function, some PHP function called count. And I’m passing in my array as a parameter. And what will happen is that this function is going to go ahead and count all of these, whatever number that is, and then when, whenever it’s finished. That operation is going to assign it So the variable size, so when I want to print the array numbers is size, then I can just print the value that comes from the variable size. And so when I refresh once again, then I see all my I have a little bit error here. So we’re getting an error, a boat’s on undefined variable. And that’s because I’m seeing number here. So that’s good. We have our first PHP error, which showing us that we have some unidentified variable on line 23. So we know exactly where to look and what we are looking for. So the error is that I have the area’s numbers, and I’m referencing number. So I just add an s there, fix that. And there we go. So now I have 20 values inside my array. And if you look at this closely, remember that we started off with this being 10 All right, no, it’s 101. Why is it 101? Because what I changed a 10 to one or one. So it’s just looking in position five and bringing back whatever value it sees that so this array subscript thing works. Now, what if I wanted to print out everything in this array every value without actually writing the subscripts? manually. So if it was five, then maybe in writing the manual would be fun, maybe 10. Okay, you could be a three, but then I have 20. And I really don’t want to sit down and write echo numbers one, it goes numbers to words starting from zero, up one to 119. So I can actually use a loop. And this is where we start seeing how our loops are actually useful. And I can say for and clear some call center and initialize it to zero then I See cones is less than, and we just got the size of the area. So I can see where the cones starting from zero, which we know is the initial subscript of any area, we can do something until it exceeds the size of the area, we just got the size of very. So we can just say count is less than size, and then count plus plus. And then for the action, what we want to do is echo and then once you use my interpolation once again, and we want to echo the variable numbers, but here’s a twist. I’m not going to hard code, the subscripts like I did here with those here, I hard coded five and here I hard coded nine. So those are never going to change. But what I want this to do is dynamically go through this Hurry from the first to the last and present me the value. So I can actually use a variable here in the subscripts because the variable can contain comparable value. Alright, so I’m just going to inject the counter, which is the variable cones right here as the subscripts. Because it starts from zero, and it goes to one, then two, then three until it is no longer less than the entire size of the array. And that’s how you start putting the pieces together. Alright, so when I do this, it will just echo echo echo until it finishes the for loop. And by that time, we should have printed out everything inside of the array. So I’m going to put these in p tags. Just make sure that they render absolutely perfectly to our HTML. And then we’re going to refresh this page and we see it’s printing one 23456 through nine and then we’ll get up start getting those crazy numbers that I had entered at some point. So the fact is using a for loop we can easily traverse and print out what is in this specialized storage construct called an array.