Placeholder Image

Subtitles section Play video

  • So let's talk about naming keys in an array.

  • So I showed you how to create an array.

  • And when you create an array, all the values in the array automatically get numeric keys so they get keys that start at zero go.

  • 12345678 And continue up as you add values to the array one the problems are gonna get in the coding world, though, is that a lot of times you get confused with what you're doing.

  • So if you're tryingto add information to an array or more importantly, if you're trying to delete information from a rich array or modify information from an array keeping track of whether you're supposed to be going for index key, three or four can get confusing.

  • Because remember, when you're dealing with an array, it automatically starts at zero for the index keys and so you can get confused.

  • So you think Okay.

  • Ah, so I need to be modifying.

  • Ah, the key three.

  • But key is key.

  • Three, actually key three or is key three actually key four because of or key to, because of how the numbering goes.

  • And that's one of the ways that you can get into a big problem.

  • And so what can make life a lot easier is if you just name what the keys are.

  • Instead of the keys being named a 0123456789 10 you can have the keys be named name and city and profession.

  • So let's say if this is a C R M software instead of having the Keys B 012345 and you put first name and zero and last name at one you know, email address or whatever.

  • And three, What you can do is you can simply name the keys and say for key named name.

  • This is the name for key named email.

  • This is the email so on and so forth.

  • So then, when you go to modify or when you go to pull information, it's a lot easier to plug in.

  • I need key name versus I Niki zero.

  • Or was it one crap?

  • I had it something.

  • Maybe it's too now, right?

  • That's when the problems that you can run into so let's go over to the computer so I can show you how to name the keys and make your life a hell of a lot easier as a coder.

  • So this is a simple script that I created.

  • Cult array keys dot PHP And as always, I opened it up with a P H P tag.

  • Now, as far as creating this rate, array is concerned there is a new way to created race.

  • But again, I'm using the old way because this will be most compatible with everybody using almost any version of PHP that they're using.

  • So there is a different way to create an array.

  • It looks a little bit night.

  • Niedere looks It's a little bit easier, but this will work for everybody.

  • So once you're comfortable with this, do a little bit research on the new way of doing it.

  • But basically all we're doing here again is we're creating a dollar sign info.

  • So this is the variable that we're going to be creating for this array, and we're going to set a equal to an array with named Kees.

  • Now what I did here, since White Space doesn't really matter a whole lot in PHP, I separated us out to different lines again to make it easier for me to read.

  • But if you wanted to put this all on to a single line, you could do that too.

  • So what we do here is we use the array function.

  • We do array, we open parentheses and then for the keys.

  • Now, what we're going to dio is we're going to single quotation mark, whatever you want.

  • The key name to be close.

  • Single quotation.

  • Mark.

  • So this is how you name a key, Then what we're going to do is this little equal sign.

  • Greater thing.

  • Kind of think that Maura's an arrow.

  • It's not.

  • It's not really like a comparison operator like we've used before.

  • Think about it.

  • More is like an arrow.

  • So we want this to be that type of deal, eh?

  • So we just put equal sign the greater sign a greater than symbol and then for the value for this key again we're gonna do is we're going to a single quotation mark.

  • We're gonna plop it, whatever it is.

  • So here we say, Bob and we close with single quotation mark.

  • We then end this particular key with a comma.

  • So that separates the keys.

  • We then go down here and again for the next key name.

  • Ah, Single quotation, Mark.

  • So for this, it's city close Single quotation mark equal, greater than sign.

  • And then for this here, we're going to say Baltimore single quotation, single quotation Baltimore in the middle And then we have a comma.

  • Then for the profession against it is like a c r m self where something like that against single quotation, mark profession or whatever you wanna put close single quotation, Mark equal, Greater sign.

  • And then we put a nightie.

  • Now, do you realize here that again, there's no comment.

  • The end since is the last one.

  • And then how we close this out is use the parentheses and you use the comma.

  • So this Ooh, this is the same as this again.

  • If you want to put all of this on a single line, you could do that.

  • The white space really doesn't matter.

  • I plotted this out here.

  • Just it was a little bit easier to see Then what we're going to dio is we're going to print out all the information in the array again.

  • It's the troubleshooting step just to make sure what's in the array is what we think is in the array and to do that for for what we're using now is we're going to use print, underscore our parentheses and then we're just going to plug in info.

  • So this is going to plug in.

  • The keen air is going to print out the key names and the key values for what's an info so we can verify what's in there is what we think is in there.

  • Course Semi Colon, that we're going to do it again.

  • We're just going to separate this out in HD Amounts.

  • Were gonna print a brake line like we normally d'oh.

  • Now, when we're going to print a key value out of this array, what we can do is we can use a name.

  • We can actually use a name of the key value, then a number that we might get confused by so we can say print dollar sign info bracket.

  • So it's a bracket.

  • And then for the key name, single quotation mark, whatever the key name is.

  • So again, this is a lot easier If I want the name.

  • I plug in name with City of Logan City, want the professional plugging profession I don't have to sit there ago.

  • Crap.

  • This city, it's, you know, a city one or city two or maybe cities through.

  • All right.

  • You know, you don't get confused, just plug in name, and that's all it is again.

  • Close.

  • Close.

  • Ah, Single quotation marks.

  • Close bracket, of course.

  • Semi colon.

  • And then you close out the close.

  • How PHP.

  • Then when you go to run this and they're ago.

  • So now we have the race.

  • So this prints out print our gives us this.

  • So it is an array.

  • The first key name is name.

  • And in there we have Bob the second KIIS city.

  • And then there we have Baltimore.

  • The third key is profession.

  • And there we have I t So we can look at this and make sure this is what it's supposed to look like.

  • And then, as we did here, we want to print out the value for name within info.

  • And so that then prints out.

  • Bob, if you want to go here and let's say we wantedto print out what the name for the city is got file save, array, key upload.

  • And then if you refresh now it's Baltimore So this is how you can put information into an array.

  • Ah, for let's say, one particular, like a variable for like a user.

  • So their first name, their last name, all of that information, and then you can simply pull out what you need.

  • It makes life a lot easier when you're trying to think about things.

  • So that's all there is the naming keys and an array.

  • Ah, whether or not you need to do this with your particular script, it really depends on what you're doing.

  • If all you're doing is gathering things like names and then want to be a bill sort names, you probably don't need to deal with naming keys, but especially if you're doing against some kind of some kind of script where you're trying to collect a number of bits of information essentially, for one variable, this is a good way.

  • You can do that with an array.

  • So again their first name, their last name.

  • You can have all of that information, and then it's very easy to pull that information out, because now you can simply say OK for, you know, dollar Sign person.

  • I want their last name instead of having a whole half, you know, 1000 different variables to collect all this information and deal with it.

  • So that's really all there is Thio naming keys in an array.

  • That's how you do it again.

  • Yeah, this is one of those things.

  • There's a new her way to do this.

  • If you want to research it, you should do that.

  • But this what I showed you now should work on whatever version of PHP you're using.

So let's talk about naming keys in an array.

Subtitles and vocabulary

Click the word to look it up Click the word to find further inforamtion about it