Placeholder Image

Subtitles section Play video

  • 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.

Subtitles and vocabulary

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

B1 array print sort sorting database php

PHP - Sort Array by Value

  • 300 3
    林宜悉 posted on 2020/03/26
Video vocabulary

Keywords

sort

US /sɔrt/

UK /sɔ:t/

  • verb
  • To arrange things in a systematic way, typically into groups.
  • To arrange things in groups according to type.
  • To organize things by putting them into groups
  • To deal with things in an organized way
  • noun
  • A category of things or people with a common feature; a type.
  • Group or class of similar things or people
incredibly

US /ɪnˈkrɛdəblɪ/

UK /ɪnˈkredəbli/

  • adverb
  • To a great degree; very; amazingly
  • To an extremely high degree; remarkably.
  • To an extremely high degree; remarkably.
  • Extremely; so much so it is hard to believe
  • To an extremely high degree; very.
  • To an extreme degree; very.
  • In a way that is difficult to believe; surprisingly.
multiple

US /ˈmʌltəpəl/

UK /ˈmʌltɪpl/

  • adjective
  • Having or involving more than one of something
  • Capable of handling more than one task or user at a time.
  • Consisting of or involving more than one.
  • Affecting many parts of the body.
  • More than one; many.
  • Having or involving several parts, elements, or members.
  • noun
  • Number produced by multiplying a smaller number
  • A ratio used to estimate the total value of a company.
  • A number of identical circuit elements connected in parallel or series.
  • A number that can be divided by another number without a remainder.
  • pronoun
  • More than one; several.
grab

US /ɡræb/

UK /ɡræb/

  • verb
  • To take and hold something quickly
basically

US /ˈbesɪkəli,-kli/

UK /ˈbeɪsɪkli/

  • adverb
  • Used before you explain something simply, clearly
  • Used as a filler word or discourse marker, often to indicate a summary or simplification.
  • In the most important respects; fundamentally.
  • In essence; when you consider the most important aspects of something.
  • Primarily; for the most part.
  • In a simple and straightforward manner; simply.
position

US /pəˈzɪʃən/

UK /pəˈzɪʃn/

  • noun
  • A condition with regard to circumstances.
  • The way in which someone or something is placed or arranged.
  • Person's opinion or attitude about something
  • A job; a post.
  • Specific location where someone or something is
  • A particular stance or attitude towards something.
  • Where you play or your role on a sports team
  • Person's job or status level
  • Set of conditions affecting what you can (not) do
  • A player's place in a team.
  • other
  • To put or arrange (someone or something) in a particular place or way.
  • To put (someone or something) in a particular place or arrangement.
  • To put (someone or something) in a particular position in relation to other people or things.
  • verb
  • To put in a particular location or direction
script

US /skrɪpt/

UK /skrɪpt/

  • noun
  • Written text of a book, play, film, or speech
  • Set of letters or characters of a written language
  • verb
  • To write a text for a movie, play or speech
current

US /ˈkɚrənt, ˈkʌr-/

UK /'kʌrənt/

  • adjective
  • In general use or accepted by most people.
  • Of or relating to the present time; up-to-date.
  • Happening or being in the present time
  • Happening or existing now; belonging to the present time.
  • Valid or up-to-date.
  • noun
  • Electricity flowing through wires
  • Currency in circulation.
  • Movement of water in a river, or air in the sky
  • A widespread feeling or opinion.
  • A general tendency or course of events.
  • other
  • A flow of electrical charge through a conductor.
research

US /rɪˈsɚtʃ, ˈriˌsɚtʃ/

UK /rɪ'sɜ:tʃ/

  • noun
  • Study done to discover new ideas and facts
  • A particular area or topic of study.
  • A department or group within an organization dedicated to conducting research.
  • A detailed report of the results of a study.
  • verb
  • To study in order to discover new ideas and facts
  • other
  • A particular area or topic of academic study or investigation.
  • The work devoted to a particular study.
  • Systematic investigation into a subject in order to discover or revise facts, theories, applications, etc.
  • The systematic gathering, recording, and analysis of data about issues relating to marketing products and services.
  • other
  • Systematic investigation to establish facts or collect information on a subject.
  • other
  • To study the market relating to marketing products and services.
  • To study (a subject) in detail, especially in order to discover new information or reach a new understanding.
  • other
  • To carry out academic or scientific research.
exist

US /ɪɡˈzɪst/

UK /ɪɡ'zɪst/

  • verb
  • To be present, alive or real
  • other
  • To be real; to have objective reality or being.
  • To live, especially in very difficult conditions
  • To occur or be found