How to process CSV files using PHP? (code example)

青灯夜游
Release: 2023-04-05 07:46:01
Original
4979 people have browsed it

In PHP we can use the fgetcsv() function to parse CSV files, which can automatically parse the CSV fields in a given resource. The following article will give you a detailed introduction to how to use PHP to process CSV files. I hope it will be helpful to you. [Video tutorial recommendation: PHP tutorial]

How to process CSV files using PHP? (code example)

What is a CSV file?

A CSV file is basically just a text file that conforms to a specific format, a plain text file format used to store data, typically used in spreadsheet or database software. In a CSV file, all values ​​are separated by commas (hence, the meaning behind the acronym: comma-separated values), while rows are separated by newlines.

Summary: CSV files consist of commas, data and newlines; newlines separate rows and commas separate values/columns.

An example of a CSV file, name it example.csv:

Ted,USA,21
Lisa,UK,23
Michael,USA,20
Louise,Ireland,30
Copy after login

As you can see: each row is separated by commas; the first column is The person's name, the second column is the person's country, and the last column is the person's age; each person is separated by a newline.

How to process CSV files using PHP?

#php can use the fgetcsv() function to parse CSV files; this function parses a line from the opened file and verifies the CSV field.

The syntax is as follows:

fgetcsv("filename.csv", 1000, ",");
Copy after login

filename.csv: The name of the CSV file.

1000: Indicates the length of the longest line.

",": Indicates the optional delimiter parameter. The default is of course comma (,).

In order to use the fgetcsv() function, we need to first use the fopen() function to open the file for reading, and then use the fclose() function to close the file to end the code. In between, we use a loop to parse each CSV row separately.

Let’s briefly introduce how to deal with it:

1. Use the fopen() function to open the file and read it:

$h = fopen("filename.csv", "r");
Copy after login

Use "r ” mode to open the file and read it; the variable $h will save the processing of data from the file.

2. Use the fgetcsv() function to read the data of the CSV file line by line, and save each line of data separately into an array named $data.

$data = fgetcsv($h, 1000, ",");
Copy after login

In order to read all lines, we need to use a while loop:

while (($data = fgetcsv($h, 1000, ",")) !== FALSE) 
{
  // 读取数据
}
Copy after login

3. The file needs to be closed

fclose($h);
Copy after login

Combine the above 3 steps:

Copy after login

Output:

How to process CSV files using PHP? (code example)

Code example: Convert CSV file to PHP multidimensional array

The following code generates a multidimensional array ( $the_big_array), each of its items consists of an array converted from a single row in the CSV file.

";
var_dump($the_big_array);
echo "
Copy after login
"; ?>

Output:

How to process CSV files using PHP? (code example)

The above is the entire content of this article, I hope it will be helpful to everyone's learning. For more exciting content, you can pay attention to the relevant tutorial columns of the PHP Chinese website! ! !

The above is the detailed content of How to process CSV files using PHP? (code example). For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!