php - 如何在 php 中显示 CSV 文件中的特定列?

标签 php file csv fgetcsv

我有一个 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/

相关文章:

python - 我有一个 SAP 生成的文件,其中包含许多列和一些不需要的行。我应该如何直接读入 Pandas?

php - 如何使用 implode stdClass 对象数组中的列?

javascript - 尝试使用 php 编写的 javascript 删除 iframe 内的元素

php - 如何将数据库/PHP 中的 PDF 文件嵌入 HTML

c - 如何在 xv6 中创建文件?

android - 在我的 Android 应用程序中包含数据文件

php - 将 Joomla 数据库结果拆分为 3 列

PHP 手册 OOP 可见性示例 - 有人可以解释一下吗

c++ - 读取 cin 时超出运行时间但从文件读取时未超出运行时间?

csv - 戈朗 : convert text dump to CSV