PHP-and-MySQL/C2/Arrays/English

From Script | Spoken-Tutorial
Jump to: navigation, search
Time Narration
0:00 Hello and welcome to the tutorial on Arrays.
0:03 Arrays allow the user to store more than one piece of data inside a variable
0:08 For example the variable days would be called as an array
0:12 Inside a pair of parenthesis we're going to have more than one value
0:18 So, I'll pick 5 values for this and have every day of the week inside each of these.
0:22 I'll have Monday, Tuesday, Wednesday, Thursday and Friday - just 5 days of the week, that saves us
0:39 Say for example day one is Monday, day two is Tuesday and so on.
0:49 Hope you get the picture
0:51 This is a much easier and more efficient way of calling a variable with more more than one piece of data inside it
00:59 Please note these can also be numbers, or whatever data that you decide you require inside them.
1:06 Now, to echo out our array, I'm going to say echo days
1:11 Now, you find that this doesn't work.
1:15 This is what happens when we open up our page
1:19 We just get 'Array' echoed out.
1:22 Now, 'Array' is not anywhere inside here.
1:24 So, what PHP has done is echoed out the fact that what we have is an array.
1:30 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.
1:41 We're going to use square brackets and we'll call the position of the element inside the array.
1:45 So, you may think this is one, two, three, four, five.
1:49 However because of the numbering system, the standard numbering system, we are starting with zero, one, two, three and four.
1:57 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.
2:06 The same with one would be Tuesday and four, which is the last element in the array, would be Friday.
2:18 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
2:26 Now, let me start from the beginning of what I want to say.
2:29 I'm going to create an array, but I'm going to create it specifically.
2:37 So, days zero equals Monday, days one equals Tuesday
2:52 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 bit of a hassle.
3:04 I mean I could say day 1 equals, you know and day 2 equals, and you could do it like that.
3:14 However, what we're still doing here is putting into an array.
3:19 So, it may still contain the same structure but we're assigning it in a different way.
3:24 So you can do it like that. Let me get rid of this. I always prefer to do it like this.
3:32 I find this much cleaner, much simpler and just to let you know - you can bring this down like this.
3:43 However if you prefer to view it, I mean if I, sorry, I'll redo that.
3:49 If I save this now, refresh, nothing's changed.
3:53 There are no errors, we still get the same structure, we've just moved it down on lines
4:00 There's the line terminator at the end of your function and not at the the end of every line, so, don't get confused with that.
4:09 Okay, so, let's now go back to this.
4:14 Okay, so, that's the basic array and how to create values for it in two different ways and how to call values for it.
4:22 So, for example if I was saying echo Today is days and then I can say zero.
4:33 Now, you can see already that I've highlighted this in context - I've highlighted that in green
4:41 Now, if you refresh this, you'll see this is Monday.
4:44 Now don't get confused, I do recommend you to use contexts.
4:48 However, this isn't the standard way that contact recognizes the coding.
4:53 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.
5:08 The way it would like you to write is, like that and we can see that's fine
5:15 But you can incorporate an array into your string in order to echo it out.
5:22 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...how can I describe that value for each sort of identity.
5:36 If you don't follow then this is the way I'm going to create it.
5:40 I'm going to say ages equals array, now inside, I'm going to say 'Alex'
6:03 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
6:16 Basically using equals and greater than sign.
6:24 Now, what this is done is instead of our array elements as in this being called by zero, this being called by one, this being called by two.
6:34 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.
6:44 So, really, it would be the same as writing that. Lets get rid of that and calling this as zero, one and two.
6:54 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.
7:05 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'.
7:22 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?"
7:38 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'.
7:50 We are not starting at zero and then one. We're starting at one and two, so that you find that is easier to remember.
8:00 So, we can now say echo, 'ages' one, that comes out as 'Alex'.
8:07 We're not using zero for that, it's a lot more user friendly for you to program than to say, zero, one, two.
8:16 Try it out - work around it - see what's easiest for you.
8:21 But I mean this to me is quite pointless because I'm in the mood of using zero, one, two.
8:27 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
8:36 Okay, that's the basics of arrays, I have another tutorial on multidimensional arrays.
8:44 Its a separate tutorial. Please watch it.
8:47 That's all in this tutorial. Thanks for watching. This is Arvind dubbing for the Spoken Tutorial project. Bye.

Contributors and Content Editors

Chandrika, Pravin1389