Placeholder Image

字幕表 動画を再生する

  • So let's talk about sword ing keys and an array.

  • So I've shown you how to sort values.

  • So you put values into no ray such as names, and then you're able to sort them alphabetically or reverse alphabetically.

  • But imagine if you're doing something Maur like an inventory type program.

  • So you have a particular product, and then you have variations of that product, and then you have an inventory count.

  • So at this point, what you might want to do is you want it might want to name your keys.

  • So let's say you have something like T shirts, so you would name the keys orange, blue, red, white, so on and so forth, and then the value of that key would be the number that you have an inventory.

  • So this will be a type of report that somebody might run so that they can see okay in the T shirts.

  • These are different types of these T shirts, and these are the numbers that we have an inventory.

  • It's basically a very, very simple type of script that may run well again.

  • One of things that you want to dio is you want to be able to present that information so that the user can't can read it, so it's comprehensible to the user.

  • And so one of things that you may want to dio is you may want to sort the keys of an array so that it's easier for the peat person to read.

  • So again, if you're like with a script that I'm going to show you, if you have colors of something like a T shirt, you sort those colors alphabetically and then they can see how many of those particular shirts that you have.

  • It's just easier, right.

  • Having having blue become come before orange when somebody's reading down a list just makes a lot more sense to the brain.

  • So one of things that you could do with an array is you consort by the key, not just by the values in the array, so that let's go over the computer and I can give you a demonstration of how this works.

  • So here we have a simple script that I created to show you how to sort the rays by keys called a ray key sort dot PHP.

  • As always, we opened with a PHP tag and what we're going to do now says this is gonna be like an inventory type simulation, so we're going to create a variable dollar inventory and then we're going to set it to the value of an array.

  • So we're going to use the array function and we're going to open parentheses.

  • Now, remember, all of this can be on a single line.

  • White space doesn't really matter in PHP.

  • I'm writing it out this way, just so it's easier for all of us to read.

  • So once you open the parentheses, the next thing that you're gonna d'oh is there any is a single quotation mark and we're gonna do orange.

  • So for inventory, we have something of orange color.

  • We're going to close single parentheses.

  • We're gonna do the equal greater sign, and then we're going to say we have three of those.

  • So we have three of orange.

  • Something's than two separate.

  • We're going to use a comma.

  • You always use a comma when we open single parentheses again.

  • They were going to have blue close single parentheses, equal greater than, And then we're going to set that toe a value of five.

  • So if three oranges, we have five blues and we're going to keep going, Comma, we're going to have seven greens.

  • We're going to have 12 reds, eight whites and one teal gets to show you how this goes again.

  • At the end, you don't put a comma, you do put a closing parentheses.

  • And of course, you put a semi colon to close all of this out.

  • So we now he have set the variable inventory dollars on inventory to this array again, these are the keys, and this is the value of the key.

  • Now what?

  • We're going to dio again just to demonstrate what the Array currently looks like is we will be do print, underscore our parentheses, and we're going to plug in inventory.

  • So we plug in inventory, they're closed parentheses, semi colon.

  • So this is going to print out the array inventory as it currently looks before we do anything to it.

  • Of course, we're going to do that whole print brake line, so puts the next results on the new line.

  • Then what we're going to do here is we're gonna nuke a sort so sorts for the values and an array K sword is sorting for the keys it was just case or parentheses.

  • Again, you dump the array in their dollar sign inventory, closed parentheses and then a semi colon.

  • So what, this has done case or this will alphabetically sort the array by keys.

  • And then what we're going to do is we're gonna do the print underscore, are and we're going to print out inventory as it now looks.

  • Of course, we do the brake brake to a new line, so it's easier to read when we actually take a look at this.

  • And then now what we're going to do is we're going to UK for key and then just are sore.

  • So basically, this is just sore at our sort with a K.

  • Pretty easy, remember, then parentheses.

  • We're gonna plug an inventory here, close parentheses, semicolon, Of course.

  • Then we're going to print our to show what the array currently looks like.

  • So this is all that you have to do in order to create a named key array, be able to print it out as it is, you're going to case sort to sort it alphabetically, and then you're going to k r sort disordered reverse alphabetically.

  • That would plug all this in a silicon dodo dot com array key sort.

  • And this is the result that we get now.

  • So the first in the beginning, this is how we said it and the actual script itself.

  • Right?

  • Orange, blue, green, red, white, teal, orange, blue, green, red, white, teal.

  • So that's how it was when we played.

  • Then the value stay the same Now when we saw alphabetically, we see blue, green, orange, red, teal and white.

  • So again, imagine once we actually show you howto print this out in a report.

  • That would just be a lot easier for somebody to read and take a look at because people just assumed blue is before agree.

  • And again, we have those that we have those numbers there for how many are supposed to be an inventory.

  • And then, for whatever reason, if we want to do a reverse sort based off the keys, we now see White, teal, red, orange, green, blue.

  • So again, obviously reverse order.

  • That's really all there is to creating a named key script and then being a bit assorted, sorted alphabetically or sort of reverse alphabetically.

  • It's pretty simple, and it really will become valuable once we actually start showing you how to print out real reports.

  • So that's all there is a sorting by keys in a row in a rain.

  • But as we start to deal with the Rays, what I want you to realize is this is where things get complicated.

  • Encoding.

  • Not from a technical standpoint, but from a mental standpoint.

  • One of the reasons a lot of coders failed or they give up is because they can't work through how they should write out scripts and order for them to work.

  • They know how to create an array.

  • They know how to do variables.

  • They know how to do functions.

  • They know they know how to type out code that works.

  • But the question starts.

  • Becoming is okay.

  • I have to go to a database.

  • I have to pull information out of that database.

  • But what am I pulling that information into pulling it into standard standard variables?

  • Am I pulling that information into an array and I pulling that information into values of Honore?

  • Or should I pull that information to be keys of the array?

  • And this is where people really do start to fail in coding, not from the technical skill set right, right in a raise is dirt simple.

  • I just showed you how to do it.

  • In about three or four videos, there's a couple of more.

  • But this is the basic stuff, right?

  • Showing you how to do this takes all about 20 minutes.

  • The rial issue that comes with coding is, once you understand how to do it, then trying to mentally work through When do you use value keys and array?

  • Where to use name keys and Ray, When do you just use variables?

  • When do you When do you use these things and order to actually produce a solution that will do whatever it is your customer or client needs you.

  • D'oh!

  • So this is where it starts to get of complicated and coding again, Not from the technical standpoint.

  • Is this very easier?

  • Nine year old could learn this in a couple of minutes.

  • The problem is, is that mental gymnastics of Okay, I have a list of names and so if I have a list of names, I'm going to do this.

  • No, but I have an inventory thing.

  • I'm going to do this, but then I have something else right?

  • It starts to get.

  • That's where a lot of people get frustrated and give up.

  • So learn how to do this and then start playing around.

  • And really, the skill that you should be learning now is thinking about how you would use thes constructs, thinking about how you would use these options in different scenarios.

  • If you're dealing with an inventory system, what would you have?

  • How do you think you'd want to go about designing that?

  • If you're dealing with some kind of again name system or attendants system again, an attendant system kind of sort of looks like an inventory system, but how you deal with that data may be entirely different.

  • So what I want you to start thinking about is how you use the data in different ways and how you think you would go about actually bringing that into a PHP program.

  • So that's Ah, that's how you sort by keys and pH in a raise in PHP.

  • Ah, and that's why it matters.

So let's talk about sword ing keys and an array.

字幕と単語

ワンタップで英和辞典検索 単語をクリックすると、意味が表示されます

B1 中級

PHP - キーによる配列のソート - ksort() および krsort() (PHP – Sort Array by Key – ksort() and krsort())

  • 1 0
    林宜悉 に公開 2021 年 01 月 14 日
動画の中の単語