Now that, you know what arrays are? Let’s look at how to use them in PHP, starting with indexable arrays. All right. My opening PHP tags. On a comment here, indexable array. And let’s create that color array. So the array is going to have a name just like any other variable with a dollar sign first. So we’ll say colors, get, and then we’re going to use the keyword array and inside of the parenthesis for the array function or keyword, we’re going to have a comma-separated list of items. We’ll say, red, blue, and green. So if we want to see what this array looks like,
we can use a special function called print r. That will print the array nicely for us. So we’ll say print our colors. I’ll save this, and then I’ll go to the browser. And you can see that the array is a simple array with our indexes as numbers, starting at 0. That means that if we want to reference a specific color, we would do so by writing Echo, colors, square bracket, and then the index.
So if we wanted to print blue we would say Echo colors square bracket, 1 close square bracket. Now, if we go back out and refresh you can see the word blue shows up here. And if we wanted to have it on its own line, we can add some HTML.
So I will add single quotes. I’ll say, Echo, single quote paragraph, tag close single quote,
use our concatenation period, And then after colors, I’ll do the same thing. Single quote and
P tag. and now, Blue is on its own line. So that is an indexable array. Something that makes PHP
arrays, especially flexible. When compared to other programming languages is the way you define and add elements. While stricter languages require you to define a size when you define the array PHP. Does not. So we’ve defined our array up here with initial values, but if we want to add a new value, we could simply say colors. Square brackets with no index get and then whatever color we want to add. So let’s say yellow. Now if we print our array, And refresh the page. We have a brand new element with index 3.
So again, with stricter languages, you might have to say that you want an array of size 4, or you’d have to name the index here, which means that you need to keep track. Mentally of how many items are in your array PHP, makes this a little bit easier. Now, let’s look at a slightly more complicated example, with associative arrays. So let’s use that. Hometowns example. I’ll create a variable called hometowns. I’ll make it an array, but we want this to be an associative array. So, whereas with the indexable array, we only need to define the values. With an associative array. We need to define the keys as well. So I’ll say Joe, and then the arrow operator, this is how we assign values in arrays, so we’ll say, Joe Arrow operator. Middletown, New York, Aaron.
And then Westchester. Notice, we also have to put a comma after every key-value pair here as well. Dave & Eton. And Brian.
From Grand Rapids. Now, I’m also going to add a comma after the last element here. This is a perfectly valid syntax and it will make adding elements to the list. If we want to easier because we won’t have to remember to add this later. And again, let’s use print to visualize this. All right, so this looks a little bit more complicated. Let’s make this a little bit pretty to do that. We will Echo our
pre tags before and after print
00:05:00 Speaker 1
Great. That looks a lot nicer and you could see that the keys are the strings that we defined and the values are the values. So now if we want to print out, let’s say Dave’s Hometown we could say,
Echo will have an R P tag, dot hometowns and then we would need to reference the string index and its case sensitivity. So keep that in mind. We used a capital D for Dave, so we’ll say hometowns square bracket, Dave, and then we’ll close our P tag here. Now, if we refresh our page, it’ll say, Eaton PA. The last thing we’ll take a look at is a multi-dimensional array and to save some time. I’ve already pointed this out for you. It is a multi-dimensional array, where it has my brothers and me with various information about each of us. So we have our brothers variable gets array. And then the first key is my name. And the value is another array with key-value pairs for age job and state. If we want to show how this is visualized, we can again print out our pre-tag here. So we’ll say Echo Prix. Print is Brothers. And then Echo and pre. Now, let’s go to that page.
So you could see the visualization looks very similar to the definition here and it looks familiar to the other arrays that we saw. But if we try to do something like for example, Echo, a key in the brother’s array, so we’ll say Echo pee. Brothers. Rob. we’ll save that if we go back to our browser,
You’ll see that we get an error here that says, notice array to string conversion and just the word array. That’s because we need to be more specific. Since Rob is a key that points to another array, we need to grab a key inside that second array to get the value. So if we want to get the state where Rob lives, we would need to add a second pair of square brackets here. So after the closing square bracket on Rob, we would open a square bracket at our quote And then grab the key to the second array. So we’ll say the state will close the square bracket will save. This will refresh and you can see the letters Florida show up. Here are the letters FL for Florida shows up here.
A raise will be a big part of working with data in your code and it’s good to get to know them as early as possible. Later,
you’ll learn how to easily Traverse through an array using loops.
Comments
Post a Comment
If you have any doubts, Please let me know