Hey guys, welcome to another PHP video. And this tutorial is about arrays. Go ahead and open up a notepad or whatever you're using. And open up your main PHP tags. Now, we’re going to go over arrays. Now basically, what an array is it is just a container for data that can hold multiple values in one single variable.
And you can give it, they each have an index number so you can always access them by their index number. And basically, I'm just going to show you how to make one right now. Create a variable. We’ll call it an array. Type an equal sign and then type the function or I don’t know if it’s really a function or not but the name is array and then you put a parenthesis. So in here, you're going to type your array values. So, we’re going to create an array of integers. Put one, two, three and four. So, basically you put a comma after each value and then the next value until you completed your array and when to stop.
And by the way, I'm just going to let you know right now there are lots of array functions. Like different stuff like a function to add a value onto the end of array, or add it on the beginning, or break an array at a certain value, or search an array for a value, or turn a string into array. There’s just a lot of them and I'm not really going to go over those right now. I might eventually go over them when I'm showing you specific things but as far as the basics go, I'm just going to stick to the basics and just show you what you need to know.
So, we have an array here called myArray equals an array of one, two, three and four. Now, how do you get access to these values? Well, each value is given an index number. And the index number starts at zero. So, this one right here is actually index number zero. And then this is index number one, three is index number two, and four is index number three. That can get really confusing at times so always remember that arrays do start at zero.
So, how do you access it exactly? Well, you type the array name. Then in brackets, you put the index number. So, if we put a two in here, zero, one, two that’s going to be a three. So, myArray index number two is going to return the value at three. If we type echo and put that on there, you're going to see that a three is going to pop up. Go ahead and save this as array.php.xampp. projects and load up Firefox, sorry I’m still on my PHP manual from the last video that I just took about 10 seconds ago.
Alright, let’s see, here it is, three. Print it out the value three on the screen because we accessed it using myArray index number two which happens to be the value of three; zero, one, two. So, that's how you do it. There’s not really much more to go over but I’ll show you an array of strings real quick.
We’ll do a “Hello world, this is myArray”. Now, we just leave that the way it is. That will print out the word “this”. And there is the word “this”. So, the array of strings, it works the same way and each value is given an index number; zero, one, two. So, the word “this” was index number two. We change this to zero to print out the word “hello world” as you can see right there.
So, it’s a really simple concept to understand, probably still saving on that, okay. That’s really all you need to know, that’s the basics of arrays. There are a lot of array functions. You can search them on Google or the PHP manual that I talked about in my—it’s actually going to be my next video because I'm really seeing it after this one. It’s the databases, mySQL select from. At the end of that, I’d go over a manual and there is a link at the end of the video or in the video description so you can download the outlined PHP full manual and get all the array functions and in fax. Since I'm really—well actually I’ll go ahead and show you guys some more stuff you can do with arrays like looping through arrays, something like that. I’ll see what this does. We’re going to do “while”, myArray. So, it’s basically saying while the array is equal to true.
I'm going to echo myArray. I don’t really know how it’s going to do. Actually, I never tried that. Whoa! Okay—out of there. I was in on limited loop. Those are always fun to come across. I need to change it before I go. Oh I got an idea. We’ll use a four loop which we learned about. Let me think, for x equals zero because array starts at zero. As long as x is less than—I can't remember how you do an array, the link of an array. I think it’s stringily, no! Hold on just a second. We’re going to work that up. Okay! I think I figured it out. There’s a function called “count”, look at the array name inside it. That should count all the values which let me see how many are there. One, two, three, four, five, six, that’s six. Let me see, the actual grade. We’ll do x plus, plus. I'm going to echo, myArray and here’s the catcher inside the brackets. We’re going to put the index number of the variable x.
So, this is going to be an index number. And as it loops through it’s going to go up and we’re just going to echo at the value of the array. I'm actually going to put a space after each one of these otherwise it’s going to be kind of cluttered. I’ll then scroll the page and see what happens. I'm not really sure what will happen though. I don’t even think it worked, to be honest. Just go away. I'm not even on that page right now. I guess it kicked me off.
So, back in the projects arrays. Hello, hello world. This is myArray. Okay, I think what it’s doing, since it’s counting the values and the arrays doing one, two, three, four, five, six because count doesn’t start at zero. Only arrays do. So, let’s put count mine as one. I think that will do it. Wait a minute. Did I echo at hello myself? Hello world is myArray. Let me see. Hello, hello world. How is that other hello getting there? I had no clue how that's even there. Oh! Wait, okay, yes right here. I echoed it up. And we actually do need a count. Men, that was confusing. Sorry guys. I didn’t mean to confuse you, okay. Hello world this is myArray.
Basically, what happen there is I had echoed hello earlier right here and then count. When I did minus one, it took off the word array which notified me that actually do need the full counts. So, count myArray. It’s basically going to count how many values is in there, which in this case zero, one, two, three, four, and five. There's actually six. So, what you know, it’ll all work out. Well, I mean, it is six but as x is going to be less than six even when it reaches that value. So, it works out. But yes, that's how you echo out every value in the array. It’s kind of like looping through the array and checking stuff. You can do a lot with arrays. That is pretty fun to mess with especially with all the array functions which I'm going to show you right now. If you have the PHP documentation manual, which is like I said watch the PHP tutorial in, select from and there’s a link. Actually, I’ll probably include the link on this video too. Just for kicks and giggles but I'm going to show you real quick.
You’ll start out whatever page you started out on this. There's going to be a link at the top that says PHP manual. Just click that so you can quickly hit the index. I will scroll down to the very bottom, right here, and click function index. Now, either work for or just type control F and type the word array. And right here, you’ll see this array function, click that.
Now, at the top, you're going to find a link that relates to the current function. It’ll give you a list. Click array function right here, and now that you're at all the functions. You just scroll through all these, read the inscription. If you want to learn about—click it. And then, read and look at the examples. That’s how you do it.
Thanks for watching the video guys qnd I will see you in the next one.
Transcription by:
Scribe4you Transcription Services