Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What is the method of converting the contents of csv files to arrays by php?

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

The main content of this article is to explain "what is the method of converting the contents of csv files into arrays by php". Interested friends may wish to have a look. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn "what is the method that php converts the contents of csv files into arrays?"

Php the contents of the csv file into an array of methods: 1, create a new PHP file; 2, create a new EXCEL document and save as a CSV file; 3, the path to the csv file stored in variables; 4, use the while loop to convert the contents of the csv file into an array.

This article operating environment: windows7 system, PHP7.1 version, DELL G3 computer

Php converts the contents of the csv file to an array:

Create a new PHP file and name it text.php.

The encoding of the definition file is UTF8 to prevent Chinese garbled when printing the array.

Create a new EXCEL document, write the contents in it, and save it as a CSV file named test.csv. The storage path of the file is in the same directory as the text.php file path.

In the text.php file, write the code. Put the file test.csv path in the $filepath variable and use the fopen () function to open the file, as follows:

Using the while loop in PHP, use the fgetcsv () function to store each line of the test.csv file in the $data array, as follows:

In order to prevent the conversion process from garbled, you need to add the following code to convert to UTF8 encoding format. Then close the file and print the $data variable using print_r () to see if the conversion is successful.

Run the code and the output is as follows (successfully converted to an array):

At this point, I believe you have a deeper understanding of "what is the method of converting the contents of csv files into arrays by php". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

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.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report