Placeholder Image

字幕列表 影片播放

  • So let's talk about how to access both named Keyes and values within an array.

  • So a lot of times when you're dealing with an array, you have unnamed keys, and basically all you're doing is you're dumping ah, lot of different values into the array, and then you'll sort aerial test against those values, however you see fit.

  • But sometimes you will also name the keys and array if you're going to be doing things such as giving values to the key names.

  • So in the example that I'm going to show you, I'm going to create named keys with people's names.

  • And then the values for those keys will be what their grades are.

  • So this is kind of like an example of like a report card type system.

  • Well, when you're going to be acts, when you're going to be using a system like this with both named Kees and values in the array that many times you want to be able to turn the name keys into variables at the same time you turn the values and the variables so that you can work with them in your script.

  • So with this, you can you can still use the four each function.

  • But when you use the four each function, you're going to grab both the name key and the values within the array.

  • So let's go over to the computer so I can show you how this works.

  • So we hear we have a simple script that I created called four each key's dot PHP.

  • And in this script for the array I have created named keys.

  • So instead of just dumping information in here, I have created names for the keys and then set the values for those those names keys.

  • So, with here, what we do again, we start with BHP.

  • So we opened PHP script, and then against in this is gonna be like a report card simulation.

  • We're going to just say dollar sign report card, and that's going to be the variable that we're gonna set for this array.

  • We're going to make it equal to use the array function.

  • And here what we're going to do is we're going to name the keys in the array.

  • So the first thing we're gonna d'oh again single quotation mark, we're going to put somebody's name Mary Close single quotation mark, then we use this thing to set the value of that particular key in the named array.

  • Equal sign, less than sign and in the value for Mary Single quotation mark we will put will put b plus apps.

  • We'll put B plus and they will close a single quotation mark and then we'll put comma.

  • So what this does is create We create the name key of Mary and set the value to be Plus we then create the name key of 10 and set the value to A We create the name a key of sue, and we set the value to see And then we close parentheses and with a semi colon.

  • So we've created an array here Mary Tim Zoo with the values of B plus A and C.

  • Then all we're going to do is we're going to use the four each function like we did before forgetting the values out of an array.

  • This time we do the parentheses, we do dollar sign, report card.

  • So with this again, with four each, you feed.

  • The first thing that you do is you feed it the name of the array that you're dealing with.

  • We're going to you as as we did before.

  • But here we're going to add two variables.

  • So the first variable is we're going to say dollar sign names.

  • So dollar sign names.

  • This is going to fill in for the named keys.

  • So Mary Tim Sue and so forth.

  • So it's going to turn the name keys into a variable called names than equal again greater than sign.

  • And then what we're going to do here is we're going to turn the values of those named keys into a variable called grade past that we just close the parentheses.

  • So we do report card as the name key will be names and the value will be grades close.

  • Of course we do squiggly brackets.

  • And so what we're going to do here is we're then going to print double quotation marks.

  • And what we can do is we just basically feed the variables that we've been created.

  • So we're going to feed it names, the dollar sign names Mary Tim Sue.

  • So on so forth.

  • We do a cold one, we do a space, and then we're going to say what?

  • They're great ISS.

  • So what?

  • The great is the value of that, The name Ki and then we're going to do a break.

  • So it goes on to the next line close double quotation marks course semi colon and then close Squiggly bracket.

  • So what is going to do is this is going to go for each.

  • So for every value within the array is going to print out the name of the key and the value for that key.

  • And then, of course, what we're gonna do is we're going to close out PHP.

  • Then all you do is you come here, you got a silicon.

  • So condos joe dot com and they do for each keys and see.

  • So this is now what we have.

  • So the name of the key Mary Tim Sue with a with a colon past it than a space and in the value.

  • So Mary had a B plus.

  • Tim had an es su had a c so you could do this with things like inventory control.

  • You can do this things like with report cards.

  • Basically, this is a way that you can use both both the name ki and the value as a way to, you know, set conditions within a script, be able to print things out, so on and so forth.

  • So that's all there is to accessing both unnamed Qi and the value with an array.

  • This could be valuable for many things, whether it's for inventory control systems, whether it's for report card systems, attendant systems.

  • Ah, whole bunch of things again.

  • Once you start dealing with a raise, the main thing to remember is the erase themselves are not complicated.

  • As I'm showing you, the code to deal with an array is relatively simple.

  • What gets complicated is how you think about the data, that how you think about the array and how do you create the array to give or do what you want it to.

  • D'oh!

  • That's where coding gets very complicated.

  • A lot of people they don't realize that coding itself programming is a rather simple task.

  • What gets complex is trying to figure out how to use the rather simple code to do what it is that you need it to do.

  • That's where everything it gets very confusing and so using the four each function to be able to grab both the name of the key and the value could be a very useful thing in PHP code, and so that's why it matters.

  • And, ah, that's how did you use it?

So let's talk about how to access both named Keyes and values within an array.

字幕與單字

單字即點即查 點擊單字可以查詢單字解釋

B1 中級

PHP - 訪問數組中的命名密鑰 - foreach() (PHP - Access Named Keys in Array - foreach())

  • 6 0
    林宜悉 發佈於 2021 年 01 月 14 日
影片單字