Hello and welcome to the tutorial on Arrays.
Arrays allow the user to store more than one piece of data inside a variable
For example the variable days would be called as an array
Inside a pair of parenthesis we're going to have more than one value
So, I'll pick 5 values for this and have every day of the week inside each of these.
I'll have Monday, Tuesday, Wednesday, Thursday and Friday - just 5 days of the week, that saves us
Say for example day one is Monday, day two is Tuesday and so on.
Hope you get the picture
This is a much easier and more efficient way of calling a variable with more than one piece of data inside it
Please note these can also be numbers, or whatever data that you decide you require inside them.
Now, to echo out our array, I'm going to say echo days
Now, you find that this doesn't work.
This happens when we open up our page
We just get 'Array' echoed out.
Now, 'Array' is not anywhere inside here.
So, what PHP has done is echoed out the fact that what we have is an array.
Now, to call a specific element inside an array, you might want to call it element, in some places they call it id tags or elements of an array.
We're going to use square brackets and we'll call the position of the element inside the array.
So, you may think this is one, two, three, four, five.
However because of the numbering system, the standard numbering system, we are starting with zero, one, two, three and four.
So, for example if I wanted to echo out Monday that would be zero, so, you put zero in that and you have Monday echoed out.
The same with one would be Tuesday and four, which is the last element of the array, would be Friday.
Okay, so, we're going to go ahead and I'm going to show you how to assign values in a different way into an array
Now, let me start from the beginning of what I want to say.
I'm going to create an array, but I'm going to create its specifically.
So, days zero equals Monday, days one equals Tuesday
Now, you may think - what's the point of this? I mean, what I'm doing here essentially is creating what I said earlier would be a bit of a hassle.
I mean I could say day 1 equals, you know and day 2 equals, and you could do it like that.
However, what we're still doing here is putting into an array.
So, it may still contain the same structure but we're assigning it in a different way.
So you can do it like that.I always prefer to do it like this.
I find this much cleaner, much simpler and just to let you know - you can bring this down like this.
However if you prefer to view it, I mean if I save this now, refresh, nothing's changed.
There are no errors, we still get the same structure, we've just moved it down on lines
There's the line terminator at the end of your function and not at the end of every line, so, don't get confused with that.
Okay, so, let's now go back to this.
So, that's the basic array and how to create values for it in two different ways and how to call values for it.
So, for example if I was saying echo Today is days and then I can say zero.
Now, you can see already that I've highlighted this in context - I've highlighted that in green
Now, if you refresh this, you'll see this is Monday.
Now don't get confused, I do recommend you to use contexts.
However, this isn't the standard way that contact recognizes the coding.
What it would look better is - you'll see that when we write it down here we could say echo 'days' and zero, you can see how that's in red to show an integer, a number.
The way it would like you to write is, like that and we can see that's fine
But you can incorporate an array into your string in order to echo it out.
Anyway, I'm going to go on to associative arrays where we're going to assign id tags in a way where we hold a value for each sort of identity.
If you don't follow then this is the way I'm going to create it.
I'm going to say ages equals array, now inside, I'm going to say 'Alex'
Now, instead of going on and saying Billy and then Kyle which are the three names I'm going to use, I'm going to say nineteen, fourteen and eighteen
Basically using equals and a greater than sign.
Now, what this has done is instead of our array elements as in this being called by zero, this being called by one, this being called by two.
This is now called as 'Alex', this is called as 'Billy' and this is called as 'Kyle' but their values are fourteen, nineteen and eighteen.
So, really, it would be the same as writing that. Lets get rid of that and calling this as zero, one and two.
And to make it a bit more friendly, and a bit easier to remember, a bit easier to call, we can now say, echo out 'ages', 'Alex', like that.
So, this will echo out nineteen, as we refresh and see there - nineteen. The same if you do it with 'Billy' and the same with 'Kyle'.
So, when you're half way through a program and instead of saying aah I'll have to go back to the top and count along each row and say "Is this zero, one, two or three I can't remember?"
This is much easier to do. Another useful way of doing this is, if I was to say array one is equal to 'Alex' and then two is equal to 'Billy'.
We are not starting at zero and then one. We're starting at one and two, so that we find that is easier to remember.
Now, we can now say echo, 'ages' one, that comes out as 'Alex'.
We're not using zero for that, it's a lot more user friendly for you to program than to say, zero, one, two.
Try it out - work around it - see what's easiest for you.
But I mean this to me is quite pointless because I'm in the mood of using zero, one, two.
But if you want to use it like I have before or like this or assign a string value to any data types then that's the way to do it
Okay, that's the basics of arrays, I have another tutorial on multidimensional arrays.
Its a separate tutorial. Please watch it.
That's all in this tutorial. Thanks for watching. This is Arvind dubbing for the Spoken Tutorial project. Bye.