In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
This article introduces the relevant knowledge of "how to read CSV content in PHP and store it in an array". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!
In this article, I'll show you how to use PHP's built-in functions to read and print the contents of an CSV file and convert it into an array. We will use fopen () and fgetcsv () to read the contents of the CSV file, and then use the array_map () and str_getcsv () functions to convert it into an array.
Brief introduction
Storing data files in comma-separated numeric (CSV) format is nothing new. In fact, because of its simplicity, it is one of the most common ways to store data-CSV files are easy to read and write and can be opened in a basic text editor. This type of file stores table data in plain text.
The example of such a file is like this.
Elias,40,nurseRehema,15,programmerBeatrice,23,doctor
This data represents the information of three people, with columns corresponding to their names, ages and jobs. Although this is a simple data format, it is tricky to read and consume.
So in this article, I'll show you how to use PHP's native functions such as fopen () to open a CSV file, how to use the fgetcsv () method to read the contents of the file, and finally how to use the array_map () function to convert the CSV file into an array.
Of course, we can use some third-party software packages to do this, but PHP's built-in native functions are very useful.
prerequisite
To continue to study this article, you need the following conditions.
PHP 5.6 or higher is installed on your machine
A PHP development environment-- XAMPP or WampServer are easy to use
Some basic understanding of PHP concept
Let's get started!
Display the CSV file as a table
In this part of the article, we will read a simple CSV file that contains several words separated by commas. At first, we will use the simple file above, and we can continue to use a large random file later. Of course, you can use Microsoft Excel or a text editor to create your own file and save it with the extension CSV.
To read the file, we first need to find it in the folder or location where it is saved. For easy access, you may want to save it in the same folder as your program file. Then we can use the fopen () function to read the file.
The fgetcsv () function then checks the CSV field from the parse line in the open file. This function takes three arguments: the file handle returned from fopen (), the maximum length of the line to read, and a special delimiter, which we call the "delimiter." This can be a comma, a semicolon, or any other delimiter.
Now let's do it in practice.
Create a file called csvtable.php where services can be provided using WAMP or XAMPP, and copy the following code.
In this file, we first open the data.csv file, which should contain some comma-delimited data. Fopen will look for this file in the same folder as csvtable.php.' The r 'parameter tells fopen to open the file as read-only.
If the file is successfully opened, fopen returns a file handle that can be used to read other files. Otherwise, it returns FALSE. Therefore, before continuing to read the file, we check that the file handle is FALSE.
The fgetcsv () file then takes the CSV field from the open file, one line at a time. We tell fgetcsv to read a maximum of 100 characters per line and use a comma delimiter as the delimiter. The words found in the file are then looped and printed to a HTML table.
The last function is fclose (), which closes the open file. This frees up the memory used by the open file and allows other processes to access the file.
Open csvtable**.php**, from the WAMP or XAMPP localhost server or run php read.php from the command line, and you can see the following output.
The first part of what we need to achieve has been completed!
Now we will skip to converting the original CSV field to an array.
Convert the original CSV file to an array
Now, there is more than one way to generate arrays. We can use the fgetcsv () function to automatically convert the contents of the CSV file to an array, or we can use array_map.
Use fgetcsv () to convert the CSV file into an array
This is similar to the example above, where we use fgetcsv () to render an CSV file into an HTML table. Let's take a look at this. Create a PHP file with the following contents.
In the above code, we created a function to read an CSV file and convert it to an array. We pass in a parameter containing the name and path of the CSV file.
Then we use the feof () function to check whether the end of the file has been reached. Before arriving, we need to use the fgetcsv () function to parse the CSV field, as we did in the example above.
Use the fgetcsv () function to convert the parsed CSV fields in the CSV file into an array and append them to the $lines [] variable one by one.
Finally, don't forget to use the fclose () function to close the open file before exiting the function.
Then, we call the readDocument function, which passes the CSV file as an argument, and then the contents of the CSV file are displayed as follows.
Use array_map () to read a CSV file
Alternatively, you can use the array_map () function to read a CSV file into an array. To do this, you use str_getcsv as the callback function. This is a built-in PHP function that parses a CSV string into an array.
Callback functions are executable code that is passed as an argument to another piece of code. This parameter is expected to be passed to its code callback later.
Here's how we use array_map and str_getcsv to map our CSV data into an array.
The above code uses the file () method to read the CSV file into an array of lines. Then, through array mapping, it calls str_getcsv () on each line and stores the data for the entire file in $csv. The str_getcsv () function parses the CSV field contents of each line into an array.
The output of the above code snippet will be the same as above.
Notice how much less code is required when using the file () and array_map () functions directly?
This is the end of the introduction of "how to read CSV content in PHP and store it in an array". Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.