B1 Intermediate 3 Folder Collection
After playing the video, you can click or select the word to look it up in the dictionary.
Report Subtitle Errors
So let's talk about using the file function to turn a text file into an array in PHP.
So if you have a text file you've been storing data into a text file.
You're going to need to try to pull that data out in order to do something useful with it.
And that's something that the file of function can help you with.
So essentially all the file function does is it turns every allying of a text file into a value in an array.
So basically it lying is zero, and the value is whatever is in line zero line one, whatever is in line one line to what's ever in line, too, so on and so forth.
So this is a good way to pull out values within a text file, especially if you're doing something like a list of names, a list of names or list of inventory items that something like that you can have the file functions scroll through, and what it will do is it will take every line in that file, turn it into a value in an array, and then once it's an array, you can then do conditional off of that array or work with that array just like a normal array, as I've showed you before.
So with that, let's go over to the computer to show you how this is.
This works because this gets to be a little need on how you can actually start interacting with files that have already been created.
So here we have a simple script that I created called four each file dot PHP.
The reason for that is there going to be using four EQ to step through the array once we've created the array with the file function?
As always, we open with the PHP tag, and the first thing that we're going to do is we're going to create the Variable Dollar Sign file.
We're going to make it equal to the results of the file function, so file function is going to create an array out of the file.
So we say, file open parentheses, single quotation mark and then we're going to give the name of the file that we're going to be reading from.
So the file that I've already created is called names dot t X T, and it's all already residing in the same directory as this script is, so I don't have to add anything else.
And they're gonna close the single quotation marks, close parentheses and then, of course, do this semi colon.
So this is all you have to do to turn a file into an array in PHP.
So what's gonna happen is the file function is going to go through for every line in the array.
It is going to set the key for the value at what the line number is.
So the first line in the array, the key will be zero.
And whatever is on that line, the value will be whatever the value is the next line, the key will be one value.
The next lanky will be to value next.
Like he will be three so on and so forth.
And so what you can then do is we can use that for each function that we've dealt with it with a raise before open the parentheses.
Now we're going to feed it the variable that we've created with this array.
So we're gonna feed it dollar sign file and then as and what we're going to do is we're going to take out the key.
So we're gonna take out the key value we're gonna say dollar sign line, underscore numb.
So as it steps through that file, it will have created a key 012345 So on and so forth, we're going to equal greater van sign and what the value for that key is.
So this is a key number, and this is the value for that key that we're going to close the parentheses.
Then we're gonna open the squiggly brackets.
Now, here just to be simple, all I'm gonna do is a print.
So this is print double quotation marks.
And so, essentially, what we're doing here is we're gonna print out the lying number, so we're gonna say whatever line number it is, so we'll start with zero.
Then I'll goto one that will go to go 23456 So on so forth, they're just a print than just a colon.
And then we're going to print out the value off that line.
Then we're going to do a break to go break to the next line, and then this will just continually loop until you get to the end of the file basically the end of the array.
Then what we're going to do just to show you, Since we have turned this file into an array, we're going to create a break.
So we're gonna break between this, give us a little space, and then what I want to do is I want to sort the file.
So again, this is an array we can use for each against the array.
And we can also do sorting against the erase.
What I'm gonna do is I want to sort the file alphabetically.
So just open parentheses again.
We feed it the file variable again, close parentheses, semicolon.
So this is now sorted the file alphabetically, and then we're going to do Here is the exact same script we had above for the four each.
But now it's going to print out alphabetically.
Now, if we go here, we take a look at the names file.
So this is the name's file.
And if we open this up oops, we can see the names that are in this file.
So imagine these names have been put in the file from a former for something else.
Maybe you just typed out these names again.
One thing that's really nice with Ph.
B is do realize you don't have to create full applications to do something you could simply typing names into a text file and then feed that text file to the pH Be script, which essentially all I did here.
And so what I did is I put the name Bob on one line, Sue on one line margin.
One line, Timo one line Jim on one line and bob another line.
And can I could just keep going through here and continually add names?
What Weaken Dio As we come to the Web?
Briles er type in Silicon does show.
Then we do for each file, and then now this is what we're able to do.
So this the script was able to read from the names that t X t file.
It takes the names that are in that file.
It goes through each line and then gives a value for the line.
It takes the name, and then it's able to print that information out so we can see online.
We have Bob on line one.
We have Sue Marge, Tim, Jim and yet another Bob and we can see the different lines here.
So again you could have this like a sign up form or something else.
Now, with this, having these a non alphabetical order isn't that useful.
So one of things that we can dio is weaken sort these into alphabetical order, since it's an array using that sort function.
So now, at zero, we have Bob at one.
We have Bob at two.
We have Jim at 3 March for Sue five TEM, so on and so forth.
And so, since we're able to turn the file into an array, were then able disorder reverse order to do that type of thing.
That's really all there is to be able to turn a file into an array and then that gives you a basic idea how you can start dealing with the array once you've done that.
That's a basic overview of the file function for how to turn a file into an array, and it kind of gives you the idea of the different types of things you can do once you've done this again, things like inventory items, things like names of people and attending an event, different things like that you're able to put that that information into a text file, and then you're able to read from it again.
An important thing to understand.
As a coder, especially when you're playing around, you don't have to create complex interfaces in order to deal with.
With scripts like this again, you you can write the names or whatever else directly into a text file and then be able to read from the text file again.
It's just a simple dot T x t file.
There's nothing special to it.
So if you're gonna be adding names or something else, you could just sit there and you can type in your 20 or 30 names and then have this script deal with it, however you want to deal with it.
So that's really all there is to the file function for turning a file into an array and PHP, and that's why it matters.
    You must  Log in  to get the function.
Tip: Click on the article or the word in the subtitle to get translation quickly!


PHP - Turn File Into an Array - file()

3 Folder Collection
林宜悉 published on March 26, 2020
More Recommended Videos
  1. 1. Search word

    Select word on the caption to look it up in the dictionary!

  2. 2. Repeat single sentence

    Repeat the same sentence to enhance listening ability

  3. 3. Shortcut


  4. 4. Close caption

    Close the English caption

  5. 5. Embed

    Embed the video to your blog

  6. 6. Unfold

    Hide right panel

  1. Listening Quiz

    Listening Quiz!

  1. Click to open your notebook

  1. UrbanDictionary 俚語字典整合查詢。一般字典查詢不到你滿意的解譯,不妨使用「俚語字典」,或許會讓你有滿意的答案喔