使用 php 读取 csv 文件的最简单方法,然后选择一个特定值

发布于 2024-09-30 05:24:26 字数 589 浏览 1 评论 0原文

预先感谢您的时间/帮助。我是一个学习 php 的新手,所以请记住这一点。

第一个问题 我需要一个读取 csv 文件的 php 脚本。

第二个问题 如何回显该文件中的特定单元格(行和行/列)

我在类似的回复中找到了此脚本,该脚本完美地读取整个文件,但我只想选择该 csv 文件上的特定单元格/值。那么我如何回显特定行或行和列。

$row = 1;
if (($handle = fopen("1.csv", "r")) !== FALSE) {
    while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
        $num = count($data);
        echo "<p> $num fields in line $row: <br /></p>\n";
        $row++;
        for ($c=0; $c < $num; $c++) {
            echo $data[$c] . "<br />\n";
        }
    }
    fclose($handle);
}

Thank you in advance for your time/help. I'm a newbie learning php, so please keep that in mind.

1st Question
I need a php script that reads a csv file.

2nd Question
How do I echo a specific cell (row and row/column) in that file

I found this script on a similar reply, the script reads the whole file perfectly, but I only want to select a specific cell/value on that csv file. So how do I echo a specific row or a row and column.

$row = 1;
if (($handle = fopen("1.csv", "r")) !== FALSE) {
    while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
        $num = count($data);
        echo "<p> $num fields in line $row: <br /></p>\n";
        $row++;
        for ($c=0; $c < $num; $c++) {
            echo $data[$c] . "<br />\n";
        }
    }
    fclose($handle);
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

长途伴 2024-10-07 05:24:26

如果需要选择几种情况,最好的办法就是在php中制作多维数组。否则,只需将一些计数器放入 while 循环中即可完成。

$row = 1;
$mycsvfile = array(); //define the main array.
if (($handle = fopen("1.csv", "r")) !== FALSE) {
    while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
        $num = count($data);
        echo "<p> $num fields in line $row: <br /></p>\n";
        $row++;
        for ($c=0; $c < $num; $c++) {
            echo $data[$c] . "<br />\n";
        }
        $mycsvfile[] = $data; //add the row to the main array.
    }
    fclose($handle);
}

echo $mycsvfile[3][1]; //prints the 4th row, second column.

If you need to select several cases, the best way is to make a multi-dimensional array in php. Otherwise, just throw a few counters into your while loops and done.

$row = 1;
$mycsvfile = array(); //define the main array.
if (($handle = fopen("1.csv", "r")) !== FALSE) {
    while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
        $num = count($data);
        echo "<p> $num fields in line $row: <br /></p>\n";
        $row++;
        for ($c=0; $c < $num; $c++) {
            echo $data[$c] . "<br />\n";
        }
        $mycsvfile[] = $data; //add the row to the main array.
    }
    fclose($handle);
}

echo $mycsvfile[3][1]; //prints the 4th row, second column.
作死小能手 2024-10-07 05:24:26

我们可以使用以下脚本根据 csv 标题创建表和表字段。我想创建一个以表字段作为标题的表,而不是打印标题。每个字段的数据可能是varchar(50)

$row = 1;
if (($handle = fopen("1.csv", "r")) !== FALSE) {
    while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
    $num = count($data);
        echo "<p> $num fields in line $row: <br /></p>\n";
        $row++;
            for ($c=0; $c < $num; $c++) {
                echo $data[$c] . "<br />\n";
            }
    }
    fclose($handle);
}

We can create a table and table fields according to the csv header with the below script. Instead of printing the header, I want to create a table with the table fields as the header. The data in each field may be varchar(50)

$row = 1;
if (($handle = fopen("1.csv", "r")) !== FALSE) {
    while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
    $num = count($data);
        echo "<p> $num fields in line $row: <br /></p>\n";
        $row++;
            for ($c=0; $c < $num; $c++) {
                echo $data[$c] . "<br />\n";
            }
    }
    fclose($handle);
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文