显示PHP中的CSV文件中最后输入的数据? [英] Display last entered data from CSV file in PHP?
问题描述
我有一个CSV文件,其中包含初始标题行和未知数量的行.行格式为:
I have a CSV file with an initial header row and an unknown number of rows. The row format is:
name_data,email_data,cell_data,dob_data
name_data, email_data, cell_data, dob_data
我想打开CSV文件,然后在表中最后输入的行中描绘数据,如下所示:
I'd like to open the CSV file and then depict the data from the last entered row in a table, like this:
Name: name_data
Email: email_data
Cell: cell_data
D.O.B.: dob_data
我正在考虑可以使用fgetcsv(),但是我不确定一旦获得数据该如何解析.
I'm thinking I can use fgetcsv() but I'm not sure how to parse the data once I get it.
有什么想法吗?
谢谢-乔
推荐答案
当您只关心第一行和最后一行时,似乎无法有效地使用fgetcsv()
解析文件的每一行.因此,我将使用 file()
和
Seems inefficient to parse every line of the file with fgetcsv()
when you only care about the first and last line. As such, I'd use file()
and str_getcsv()
:
$rows = file('data.csv');
$last_row = array_pop($rows);
$data = str_getcsv($last_row);
// loop and output data
注意:您可以通过解析$rows[0]
对标头使用相同的逻辑.
Note: You can use the same logic for the headers by parsing $rows[0]
.
这篇关于显示PHP中的CSV文件中最后输入的数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!