Locked lesson.
About this lesson
Exercise files
Download this lesson’s related exercise files.
Arrays.docx58.9 KB Arrays - Solution.docx
59.1 KB
Quick reference
Arrays
Arrays are containers that hold many things.
When to use
An array is a container, like a variable. But unlike a variable, arrays hold many things at once.
Instructions
There are a couple of different arrays (indexed arrays and associative arrays).
Items in Indexed arrays are numbered, starting with zero.
$pizza_toppings = array("pepperoni", "cheese", "onion");
echo $pizza_toppings[1]; // will list "cheese"
Items in Associative arrays are key and value pairs.
$ages = array("John"=>"39", "Tim"=>"42", "Mary"=>"29");
echo $age["John"];
Hints & tips
- Arrays are containers that hold many things
- They hold strings, numbers, variables, other arrays, and more
- Indexed arrays use numbers
- Associative arrays use key/value pairs
- Items in an Array are numbered, starting with zero.
- 00:04 In this video I wanna talk about arrays.
- 00:07 And arrays are very similar to variables.
- 00:09 Variables are like the very first thing we talked about in this course,
- 00:13 the difference is arrays can hold a bunch of different things,
- 00:16 where as variables can only hold one thing.
- 00:19 A variable is a container or a bucket that holds one thing, and
- 00:22 array is a container or a bucket that holds lots lots of things.
- 00:25 PHP has a couple of different arrays that you can use.
- 00:28 One is called an Indexed Array,
- 00:30 and another one is called an Associative Array.
- 00:32 Technically there's a third Multi-dimensional Arrays.
- 00:35 That's just an array filled with other arrays.
- 00:38 We're not going to talk about that.
- 00:39 That's a little crazy.
- 00:40 But in this video we'll talk about Index and Associative Arrays.
- 00:43 So an indexed array is based on numbers.
- 00:47 Let's just create one and you'll see.
- 00:48 Let's call this pizza underscore toppings, and so far it looks like a variable.
- 00:55 We just type the word array, and then that.
- 01:00 Now when we put stuff into this, we put it in like this.
- 01:03 Let's say pepperoni,
- 01:08 cheese, Mushroom.
- 01:13 So we've got three things in our array right now.
- 01:16 So if we save this, come back here and hit reload,
- 01:19 nothing happens cuz we haven't actually output anything.
- 01:22 So let's echo our pizza toppings, save this.
- 01:27 Now if we hit reload it just says array.
- 01:30 Well what we need to do is actually designate what we want to pull out of this
- 01:35 bucket, this container, this array.
- 01:38 So we have three things in there, what do we want to echo out?
- 01:41 Well to do that you just use these straight brackets and
- 01:45 this is an Indexed Array.
- 01:46 Each of these items pepperoni, cheese, and
- 01:49 mushroom has an index number, and they start with zero.
- 01:53 Pepperoni is the zeroth item.
- 01:55 So if we wanted to echo that out, we just type in zero.
- 01:58 Cheese is the first item.
- 02:00 Mushroom is the second item.
- 02:02 That's a little confusing to people at first, because there's three things
- 02:06 you would think would be 1, 2, and 3 but it's really 0, 1, and 2.
- 02:09 If we save this and I'll hit reload.
- 02:12 Boom, pepperoni.
- 02:13 If we change this to two, it's gonna be mushroom.
- 02:17 So that's arrays, now in Associative Array is very similar except for
- 02:22 instead of numbers for the index, we used actual letters.
- 02:27 Let's go names equals array, so far it's very similar.
- 02:32 Now let's save, no let's change this to ages, let's go age and let's go.
- 02:41 John.
- 02:42 Now we do this hash rocket its an equal to and a greater than sign,
- 02:47 and let's go, oops 39, I'm 39 years old and let's go Tim hash rocket 42.
- 02:53 And finally Mary who is 29 semicolon,
- 02:58 now in order to do this, lets echo out our age.
- 03:04 And to pick one of these, we grab one of these names.
- 03:07 So I'm just gonna go John.
- 03:11 If we save this, come back here and hit reload boom, 39.
- 03:15 Actually, we're still echoing our pizza.
- 03:18 Comment that out.
- 03:19 That's a comment by the way, forward two forward slashes so, 39.
- 03:23 Likewise, we could change this to Mary and get 29.
- 03:29 So, these are sort of keys and values.
- 03:32 John is the key value 39, Tim key.
- 03:36 Value 42.
- 03:37 Mary key, value 49.
- 03:39 Key value pairs is basically what we call them.
- 03:42 These are the two main types of arrays, and you could put anything into an array.
- 03:46 Here, we're putting strings, right?
- 03:48 We could just as easily put a number or we could put another variable,
- 03:52 you could put all kinds of things into arrays.
- 03:55 And as we go on you’ll see other uses for arrays, but arrays are very important,
- 04:00 they’re another one of those just fundamental programming things.
- 04:04 That doesn't matter what programming language you're using they all
- 04:07 have arrays.
- 04:07 You're always gonna use them.
- 04:08 And they're just very useful and important.
- 04:11 In the next video we're gonna talk a little bit more about arrays.
- 04:13 We're gonna talk about sorting arrays, and some other things.
- 04:16 So that's all for this video.
Lesson notes are only available for subscribers.