So now let's talk about one of the most popular features of PHP, arrays. PHP arrays are just plain awesome. They are inspired by Perl. They're called associative arrays, Perl had associative arrays. And what happens is, part of what programmers have to do is, you have to write algorithms, which is code and steps that you write through and then the data. And so, you solve problems with a combination of the logic that you use and the data that you create. Data structures are how you shape the data. And in a language like C, they had this thing called structs, which are shaped data items. In C++, you have objects, which are shaped data items. Java has an object which is a shaped data item, and a data item has a shape. Those are all really complex ways, lots of syntax to say, this person has a first name, and a last name, and a phone number. And arrays, associative arrays, are key-value pairs. So you say, this person has a first name, and a last name, and a phone number, and you're done. And so they're just a way for programmers, without learning a bunch of extra syntax, to create data structures when they hardly even know it. And so that's what Python dictionaries are very popular, one of the most popular features in Python. Java has things like HashMaps, Windows has things called Property Bags. They are all a version of an associative array or a key-value pair. PHP arrays are probably my absolute favorite. When I move from PHP to Python I'm like, dang it, I want these things to stay in order. So PHP arrays are just my favorite associated arrays. In PHP they can either be a linear list indexed by numbers. Or they can be key-value pairs like first name Chuck, last name Severance, phone number blah. And they have two-dimensional arrays but they're really just arrays within arrays of arrays basically. We'll talk a little bit about that but not too much. So you can make an array that is a linear list of things. So here we're going to make an array, so this is sort of a constructor. It says, make me an array that has two things in it. And like most civilized languages, the first element is in [0]. So we use the sub operator or index operator or item operator. So we say, give me the [1], which is actually the second one, so we print out There. So we make an array, and then we reference an item within that array. Unless we tell it otherwise, it just puts these things in its 0 and 1. The thing that people really love though, is when you do key-value, and so this is the syntax. Let's make an array, and I read this as maps to. So this key of name maps to the value of Chuck, so this is the key and the value. The key, of course, maps to Web Applications for Everybody. And then you can use the index operator, again, to go look up the piece under the key course, and out comes Web Applications for Everybody. I love this, I love it, I love it. Sorry, I'm a fan of key-value arrays, because, like I said, it allows us to build data structures without thinking too much about it. Although later when you do object-oriented in C, you don't want to get so complex with your data structures using arrays, but that's okay. So there is a couple of ways to print these things out, because we start building shapes, so I mean shapes of data and conventions. You are the one as a programmer, you decide you call it name or course or whatever. But after a while you make these shapes and this is a super simple one. So you want a way to print this stuff out, print_r. And I'm going to put a pre tag out so that the new lines don't get all messed up, because that's HTML for pre tag. So that I see actually the way it prints out. And so it doesn't do line wrapping on all this stuff. So print_r goes through this array and prints the key and the value, key value, key value and goes through it. You'll notice that the order is the same, both when I put it in and when I take it out. That's one thing that I like. The r in print_r I think stands for recursive which means you have array within an array within an array. It'll kind of go through that and it'll indent even further and further and further, this is very simple. But you can print out much more complex structures, and you do end up with very complex structures. Sometimes you have a linear array with integer keys inside of a key-value array. Especially when you start talking APIs and JSON and stuff like that, so you want a print_r. Another thing that is even more verbose and detailed because it talks more about the type is a thing called var_dump. var_dump, I think of it as lower level. It's less pretty but it's more explicit. And what this says is this $stuff is a two item array. And the first item is name, maps to a 5 character string of the value of Chuck. course is the first key that maps to a 5 character string, SI664. So it's just a little more verbose. And I tend to use var_dump when I'm really digging through something. because print_r makes it prettier, but sometimes you lose some detail. The one thing I love about var_dump is that you can actually print false. You can actually print false, because var_dump is printing both the type and the value. So here I have FALSE is in $thing. I print out One, I use print_r and it's absolutely nothing. And this is like aah, [SOUND]. I'm so mad because I look at this and I say the code didn't run. But it did, this code did run. So when you print FALSE, it doesn't show up. Have I told you that before? Again, that's cause I am really usually angry after 20 minutes of wasted time because I keep printing stuff out. Now I can't see it. But then I realized I should have watched my own lecture and use var_dump, because var_dump says it's a false and it's boolean. There's a reason it does it this way, I'm sure, but it's not very convenient for debug printing. Now you could construct an array a couple different ways. You can start with an empty array and append stuff to the end. So this is make me an array. And then this is go to the end and add one, an item. And then it just adds these in integer positions, so 0 and 1, so it's a linear list. You can do the same thing with key values. You just put the index operator on the left hand side of the assignment statement, make an array. Stick Chuck under the key name, stick WA4E under the course Order. Smiley face. Stays in order. Okay, looping, so this is looping through a key-value array. So it's a looping construct that has two iteration variables for each, syntactically different. The array is the first parameter, and then as is a keyword. And then you can have two iteration variables, one for the key and one for the value. And so I read this as foreach $stuff as key maps to value. And that's what you put right there, it's not a less than or a greater than or equal It's a map to I think of that as an arrow, right? And I think that's exactly what the design of the language was. So this loop is going to run and $k is going to go through the keys, $v is going to go through the value. And then the next time through the loop they're going to both simultaneously advance. And so I think this is really pretty syntax. I think that's really nice syntax, and I like it. I like being able to loop through arrays. If you have a linear array, you can get it to key in a value. In this case the Key is 0 and 1, because that's how this works. This is the 0 position, that's the 1 position. You can also, on a linear array. You gotta be careful that it's a well formed linear array. You can use a counted loop, right? So count is a function that tells you how many things there are. So in this case that's going to be 2, because there's 2 things in there. So I'm going to have a iteration variable for $i = 0, $i < count, so that means it's going to be 0 and 1. [2] wouldn't work because this is an array [0] and an array [1] there. So then I'm going to add 1, so this is a counted loop that's going to run twice. It's a little uglier syntax wise, for this particular thing I would have used the foreach instead, but you can do counted loops. Sometimes you actually need a counted loop because you need the variable $i for some reason or another. And so just then you're just going to dereference $stuff[$i] which is the 0 and the 1, and then the loop prints all the stuff out. So that's a counted loop that's going to iterate through an array. Just talk briefly about two dimensional arrays. They're really not two dimensional arrays, they are nested arrays. They are recursively nested arrays so you can put an array within an array, right? And so, this is outer array, so this is the outer array and this outer array has three things. The first one, the second one and the third one. And this paper maps to an array. See the comma? The comma's part of the outer array. So there are three things in the outer array. So you can think of this as $products[“pens”]. $products[“pens”] is going into this outer array and grabbing this thing. And then, [“marker”] is within this array grabbing [“marker”] and looking up Markers, so that pulls out Markers. So it's not really two dimensional, arrays, it's array within arrays. And you won't build the structure often. And if you're going to get data from a database maybe, or you're going to get data off the web or something, read some JSON and parse it, or something you'll get this. And then you'll just have to figure out how to dig into it, how to go in and dig it, dig through this whole thing. So that's arrays within arrays, that is kind of like two dimensional arrays. So up next, we're going to talk about how there's a set of functions that help us build arrays and search them and do various things.