Home > Backend Development > PHP Tutorial > How to process CSV files using PHP? (code example)

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

青灯夜游
Release: 2023-04-05 07:46:01
Original
5284 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:

<?php
// 打开文件并阅读
if (($h = fopen("example.csv", "r")) !== FALSE) 
{
  // 将每行数据都转换为本地$data变量并储存
  while (($data = fgetcsv($h, 1000, ",")) !== FALSE) 
  {
   //输出每行数据
    var_dump($data);
  }
  // 关闭文件
  fclose($h);
}
?>
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.

<?php 
$filename = &#39;example.csv&#39;;
// 用于保存所有数据的多维(嵌套)数组
$the_big_array = []; 
// 打开文件并阅读
if (($h = fopen("{$filename}", "r")) !== FALSE) 
{
  // 文件中的每一行数据都被转换为我们调用的单个数组$data
  // 数组的每个元素以逗号分隔
  while (($data = fgetcsv($h, 1000, ",")) !== FALSE) 
  {
    // 每个单独的数组都被存入到嵌套的数组中
    $the_big_array[] = $data;
  }
  // 关闭文件
  fclose($h);
}
// 以可读格式显示代码
echo "<pre class="brush:php;toolbar:false">";
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