我有一个 CSV 文件,其中有 10 列(A 到 J)。现在,我的问题是我想使用 PHP 在屏幕上仅显示列名 E 中的数据。
如何读取并显示 CSV 文件中的这一列?
最佳答案
您可以使用fgetcsv()
函数。它获取 csv 文件的一行并将其扩展为数组。以下脚本将打印 csv 文件每行的第四列。您只需更改 $col
变量即可打印出不同的列。
<?php
//column to print, E would be 5th
$col = 5;
// open the file for reading
$file = fopen("yourfile.csv","r");
// while there are more lines, keep doing this
while(! feof($file))
{
// print out the given column of the line
echo fgetcsv($file)[$col];
}
// close the file connection
fclose($file);
关于php - 如何在 php 中显示 CSV 文件中的特定列?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25356606/