【问题标题】:How to display specific column from CSV file in php?如何在 php 中显示 CSV 文件中的特定列?
【发布时间】:2014-10-10 23:18:18
【问题描述】:

我有一个 CSV 文件,它有 10 列(A 到 J)。现在,我的问题是我只想使用 PHP 在屏幕上显示列名 E 中的数据。

如何读取和显示 CSV 文件中的那一列?

【问题讨论】:

    标签: php file csv fgetcsv


    【解决方案1】:

    您可以使用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);
    

    【讨论】:

    • 解析错误:语法错误,意外'[',期待','或';'在第 13 行的 /opt/lampp/htdocs/csv/port_cat_sidebar.php ......我认为这是错误 - echo fgetcsv($file)[$col];.....你能给我我提到的确切输出?
    • 仔细检查所有分号。
    • 经过一些修改,这是代码....你能检查一下吗?
    • 好的,没问题。如果我想显示单元格没有 [B][5] 那么显示相同输出的代码是什么。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-06-02
    • 1970-01-01
    • 1970-01-01
    • 2022-01-14
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多