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