Placeholder Image

字幕表 動画を再生する

  • So let's talk about sorting values and an array.

  • So one of the valuable things with an array is that you can have a variable with multiple values in it.

  • So you can do this for things such as containing a list of names of people that may be attending an event or doing something so you can have your people equals Bob and Sue and Marge and Tim and John, so on and so forth.

  • So this one variable can have multiple values.

  • Then what you can do with that is you can do things such as sorting.

  • So if you want to print out a list of the names in alphabetical order, you can use the sort function to sort the array.

  • If you want to sort them in reverse alphabetical order, you can use the R sort function so this could be valuable if you're grabbing.

  • If you're grabbing values out of something like a database, so you're going to a database, and based on certain criteria, you're going to pull out people's names.

  • Well, when it goes to the database is just going to pull out names as it comes to those names in the database and then you want an easy way to read through those names so you can pull the names out of the database, dump those names into an array, Then you can sort the array alphabetically and then print out that sordid array.

  • So this is one of the things that makes a raise very valuable in the real world for any kind of thing where you're printing out lists or doing that type of thing, a raise, just make life really simple.

  • So with that, let's go over the computer, and I can show you how to both so sore in alphabetical order and soar in reverse alphabetical order, an array.

  • So we hear we have a simple PHP script that I've that I've titled Array sort dot PHP And again, as we always do, we open with the PHP tag.

  • From there, we're going to create the variable for the array, and they were going to set the value of the array.

  • So for this again, what we're going to be doing is we're going to be sorting names.

  • That's kind of a standard thing that you could do with an array, and so we're going to create the variable name dollar sign names.

  • We're going to set that to be equal.

  • We're going to use the array function and then we're going to create this array of multiple names.

  • Now again, in a real programming world, this array would most likely be can be created by going to a database and pulling out names.

  • They're goingto a file on pulling out names.

  • But here we're simply just plugging in those names to make life a little bit easier.

  • So we do array in order to create the array and the do parentheses.

  • Single quotation mark.

  • And then we put it when we start putting in names.

  • Bob Close Single quotation Mark.

  • Remember the Kama Remember the comma Single quotation mark Next name, Close comma, Single quotation mark Next name Close comma So on and so forth And you can make this list Oh, just about as long as you want to make it Then at the end do you remember Here there is no parentheses.

  • E So the final parentheses E is here.

  • There's no parentheses at the end or there's no coms are There's no comma at the end.

  • There is a closing parentheses ee Ah, and then we close with this semi colon from there.

  • What we're going to do just to show you that this array exists in its current condition, we're going to use the print, our function, print underscore are and we're going to do parentheses.

  • We're going to feed it names.

  • We're gonna feed it the variable names, close parentheses, e semi colon.

  • And so what this will do is it will print out the array, as it currently is, so we can see that this is how it currently is.

  • We're gonna put a break in there like we normally do, so it makes it easier to read in the Web browser.

  • Now, where we're going to do is we're going to use the sort function.

  • So what this is going to do is this is going to sort the array alphabetically.

  • So we do sort open parentheses.

  • We dump in the variable that we're dealing with the array that we're dealing with, so dollar sign names, close parentheses and then semi colon.

  • So what this is going to do is this is going to sort these values alphabetically.

  • Then what we're going to do now is we're going to use that print our function like we did before.

  • And we're going to print out the array as it looks now after it's been sorted, then passed that we're going to do the break again.

  • Put this on a new line.

  • Now we're going to use the R sort function, so sort sorts alphabetically are sore source.

  • Reverse alphabetically.

  • Then we do parentheses again.

  • Feed at the feet of the array that we're dealing with close parentheses, semi colon.

  • And then finally, again, we're going to print underscore our function for this array to show how the Array currently looks.

  • And it should be a reverse alphabetical order.

  • Now, if we go over to a Web browser, yeah, we type in silica dough, Joe.

  • We can do type and array sort.

  • And here ago.

  • And so this is the output of this particular script.

  • So as we can see, the first time we use print underscore are to print out the array, we see its array, and we see Bob is in the first position.

  • Sue, the second had the third Ralph, which is obviously alphabetically before Ted.

  • Right?

  • So this is this is basically how we put it in and the script.

  • Then when we sort alphabetically, we can now see a Bob is in the first position.

  • Fred is in the second position.

  • Marge, Mark, Ralph, Sue and Ted.

  • So obviously this has been sorted alphabetically.

  • Then for the reverse alphabetical order we take a look at that are sort.

  • Now, Ted is in the beginning.

  • Sue Ralph, Mark, March, Fred and Bob.

  • And so this is how the Array originally looked.

  • This is how it looked after it was alphabetically sorted.

  • And this is how it looks after was reverse alphabetically sorted.

  • All that is done with this very simple code.

  • Again, we create the array we set the values we news print underscore our to print out the array We didn't sort so it's sort parentheses and the, uh, the array in order to sort it print out.

  • The results are sort to reverse a sort print are to print out the results.

  • And so that's how this code works.

  • So that's really all there is to sorting values and an array.

  • There actually is a little bit more to this, but I think it might get a little confusing for people, especially if you're watching this level of class.

  • So if you're interested mawr in sorting and a raise, I would definitely say you should You should look it up and do some research, but this gives you an override all idea on how to sort it.

  • All right, now, this may not seem very important right now, he said There you look at it.

  • Go.

  • Okay, You're sorting a couple of names.

  • That doesn't really seem like that big a deal.

  • But do remember what PHP primarily does is it basically pushes and pulls data?

  • You take data from a former from a file or from a database, you put it into a database generally.

  • And then what you're doing is you're creating reports really?

  • Like, you know, 80% of what most PHP scripts do is they create reports as one important thing that you have to do when you create a report is creating a report that makes sense for people to be able to read and look at so simply by being able to do something like sorting the information alphabetically that just makes that report so much easier to read for whoever has to deal with it in the business environment, so something like sorting and a rave is one of those things in PHP that seems very small.

  • But once you start building out larger projects, you realize, Oh, this very small thing actually is incredibly important.

  • So that's all there is to sorting and the values in an array, and that's why it matters.

So let's talk about sorting values and an array.

字幕と単語

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

B1 中級

PHP - 配列を値で並べ替える (PHP - Sort Array by Value)

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