如何解析 csv 文件以首先获取列名,然后获取与其相关的行?

发布于 2024-11-02 04:42:47 字数 2327 浏览 1 评论 0原文

这是我的 csv

column1,column2,column3,column4,column5
column1_row1,column2_row1,column3_row1,column4_row1,column5_row1
column1_row2,column2_row2,column3_row2,column4_row2,column5_row2
column1_row3,column2_row3,column3_row3,column4_row3,column5_row3
column1_row4,column2_row4,column3_row4,column4_row4,column5_row4
column1_row5,column2_row5,column3_row5,column4_row5,column5_row5
column1_row6,column2_row6,column3_row6,column4_row6,column5_row6
column1_row7,column2_row7,column3_row7,column4_row7,column5_row7
column1_row8,column2_row8,column3_row8,column4_row8,column5_row8
column1_row9,column2_row9,column3_row9,column4_row9,column5_row9

第一行当然是列名称。我尝试了 fgetcsv() 但所做的只是显示所有行。而不是我想要的。我该怎么做?

因此,如果我最后将数据放入数组中,我将能够打印出数据的表格格式,就像 Excel 中显示的那样。

谢谢,

这是我的示例:

$filename = "upload/sample.csv";
if (($handle = fopen($filename, 'r')) !== FALSE)
{
    while (($row = fgetcsv($handle, 1000, ",")) !== FALSE) 
    {
       print_r($row);
     }
}

这是我的输出:(我将 $row 放入 pre 中,以便我可以显示它)

Array
(
    [0] => column1
    [1] => column2
    [2] => column3
    [3] => column4
    [4] => column5
column1_row1
    [5] => column2_row1
    [6] => column3_row1
    [7] => column4_row1
    [8] => column5_row1
column1_row2
    [9] => column2_row2
    [10] => column3_row2
    [11] => column4_row2
    [12] => column5_row2
column1_row3
    [13] => column2_row3
    [14] => column3_row3
    [15] => column4_row3
    [16] => column5_row3
column1_row4
    [17] => column2_row4
    [18] => column3_row4
    [19] => column4_row4
    [20] => column5_row4
column1_row5
    [21] => column2_row5
    [22] => column3_row5
    [23] => column4_row5
    [24] => column5_row5
column1_row6
    [25] => column2_row6
    [26] => column3_row6
    [27] => column4_row6
    [28] => column5_row6
column1_row7
    [29] => column2_row7
    [30] => column3_row7
    [31] => column4_row7
    [32] => column5_row7
column1_row8
    [33] => column2_row8
    [34] => column3_row8
    [35] => column4_row8
    [36] => column5_row8
column1_row9
    [37] => column2_row9
    [38] => column3_row9
    [39] => column4_row9
    [40] => column5_row9
)

here is my csv

column1,column2,column3,column4,column5
column1_row1,column2_row1,column3_row1,column4_row1,column5_row1
column1_row2,column2_row2,column3_row2,column4_row2,column5_row2
column1_row3,column2_row3,column3_row3,column4_row3,column5_row3
column1_row4,column2_row4,column3_row4,column4_row4,column5_row4
column1_row5,column2_row5,column3_row5,column4_row5,column5_row5
column1_row6,column2_row6,column3_row6,column4_row6,column5_row6
column1_row7,column2_row7,column3_row7,column4_row7,column5_row7
column1_row8,column2_row8,column3_row8,column4_row8,column5_row8
column1_row9,column2_row9,column3_row9,column4_row9,column5_row9

first row is the column names of course. i tried fgetcsv() but all that would do is display all rows. rather than what i want. how can i do it?

so if i were to put the data into an array at the end i would be able print out a table format of the data just like its shown in excel.

thanks

this is my sample:

$filename = "upload/sample.csv";
if (($handle = fopen($filename, 'r')) !== FALSE)
{
    while (($row = fgetcsv($handle, 1000, ",")) !== FALSE) 
    {
       print_r($row);
     }
}

this is my output: (i put the $row into a pre so i can show it)

Array
(
    [0] => column1
    [1] => column2
    [2] => column3
    [3] => column4
    [4] => column5
column1_row1
    [5] => column2_row1
    [6] => column3_row1
    [7] => column4_row1
    [8] => column5_row1
column1_row2
    [9] => column2_row2
    [10] => column3_row2
    [11] => column4_row2
    [12] => column5_row2
column1_row3
    [13] => column2_row3
    [14] => column3_row3
    [15] => column4_row3
    [16] => column5_row3
column1_row4
    [17] => column2_row4
    [18] => column3_row4
    [19] => column4_row4
    [20] => column5_row4
column1_row5
    [21] => column2_row5
    [22] => column3_row5
    [23] => column4_row5
    [24] => column5_row5
column1_row6
    [25] => column2_row6
    [26] => column3_row6
    [27] => column4_row6
    [28] => column5_row6
column1_row7
    [29] => column2_row7
    [30] => column3_row7
    [31] => column4_row7
    [32] => column5_row7
column1_row8
    [33] => column2_row8
    [34] => column3_row8
    [35] => column4_row8
    [36] => column5_row8
column1_row9
    [37] => column2_row9
    [38] => column3_row9
    [39] => column4_row9
    [40] => column5_row9
)

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

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

发布评论

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

评论(6

心房的律动 2024-11-09 04:42:47

要一次读取全部内容,您可以使用:

$csv = array_map("str_getcsv", file("file1.csv",FILE_SKIP_EMPTY_LINES));
$keys = array_shift($csv);

要将所有行转换为一个很好的关联数组,您可以应用:

foreach ($csv as $i=>$row) {
    $csv[$i] = array_combine($keys, $row);
}

For reading it all at once you can use:

$csv = array_map("str_getcsv", file("file1.csv",FILE_SKIP_EMPTY_LINES));
$keys = array_shift($csv);

To turn all the rows into a nice associative array you could then apply:

foreach ($csv as $i=>$row) {
    $csv[$i] = array_combine($keys, $row);
}
虚拟世界 2024-11-09 04:42:47
// Opening the file for reading...
$fp = fopen('path_to_your_file.csv', 'r');

// Headrow
$head = fgetcsv($fp, 4096, ';', '"');

// Rows
while($column = fgetcsv($fp, 4096, ';', '"'))
{
    // This is a great trick, to get an associative row by combining the headrow with the content-rows.
    $column = array_combine($head, $column);

    echo $column['column1'];
}
// Opening the file for reading...
$fp = fopen('path_to_your_file.csv', 'r');

// Headrow
$head = fgetcsv($fp, 4096, ';', '"');

// Rows
while($column = fgetcsv($fp, 4096, ';', '"'))
{
    // This is a great trick, to get an associative row by combining the headrow with the content-rows.
    $column = array_combine($head, $column);

    echo $column['column1'];
}
清醇 2024-11-09 04:42:47

我使用 fgetcsv 制作了这个快速解决方案,工作正常,并且更易于阅读和理解。然后整个结果保存在 $csv 数组中

$csv = array();
$i = 0;
if (($handle = fopen($upload['file'], "r")) !== false) {
    $columns = fgetcsv($handle, 1000, ",");
    while (($row = fgetcsv($handle, 1000, ",")) !== false) {
        $csv[$i] = array_combine($columns, $row);
        $i++;
    }
    fclose($handle);
}

I made this quick solution using fgetcsv, works fine and it's easier to read and understand. Then the whole result is saved in the $csv array

$csv = array();
$i = 0;
if (($handle = fopen($upload['file'], "r")) !== false) {
    $columns = fgetcsv($handle, 1000, ",");
    while (($row = fgetcsv($handle, 1000, ",")) !== false) {
        $csv[$i] = array_combine($columns, $row);
        $i++;
    }
    fclose($handle);
}
合久必婚 2024-11-09 04:42:47

难道您不能先执行一个 fgetcsv 调用来获取标头,然后启动一个循环来获取其余部分吗?

修改了手册中的示例。它应该写出一个带有标题和值的表格。

<?php
$row = 1;
if (($handle = fopen('test.csv', 'r')) !== FALSE)
{
    echo '<table>';

    // Get headers
    if (($data = fgetcsv($handle, 1000, ',')) !== FALSE)
    {
        echo '<tr><th>'.implode('</th><th>', $data).'</th></tr>';
    }

    // Get the rest
    while (($data = fgetcsv($handle, 1000, ',')) !== FALSE)
    {
        echo '<tr><td>'.implode('</td><td>', $data).'</td></tr>';
    }
    fclose($handle);
    echo '</table>';
}
?>

Can't you just do one fgetcsv call first to get the headers, and then start a loop to get the rest?

Modified the example found in the manual. It should write out a table with headers and values following.

<?php
$row = 1;
if (($handle = fopen('test.csv', 'r')) !== FALSE)
{
    echo '<table>';

    // Get headers
    if (($data = fgetcsv($handle, 1000, ',')) !== FALSE)
    {
        echo '<tr><th>'.implode('</th><th>', $data).'</th></tr>';
    }

    // Get the rest
    while (($data = fgetcsv($handle, 1000, ',')) !== FALSE)
    {
        echo '<tr><td>'.implode('</td><td>', $data).'</td></tr>';
    }
    fclose($handle);
    echo '</table>';
}
?>
哆兒滾 2024-11-09 04:42:47

看起来它可能会将所有内容视为一行。也许您的 csv 的行结尾与应有的不同?至少你的输出是这样的。如果查看结果数组中的第 1 行、第 5 列,它包含行结尾和第 2 行中的第 1 列。其余部分相同。它将所有内容读为一行。

注意:如果 PHP 在读取 Macintosh 计算机上的文件或由 Macintosh 计算机创建的文件时无法正确识别行结尾,请启用 auto_detect_line_endings 运行时配置选项可能有助于解决问题。 -- fgetscv

你在什么平台上?无论哪种方式,请检查行结尾。

It looks like it might be taking it all as one line. Perhaps your csv has a different line ending than it should have? At least that's what it looks like in your output. If you look at row 1, column 5 in your result array, it contains a line ending and column 1 in row 2. And the same in the rest. It reads it all in as one line.

Note: If PHP is not properly recognizing the line endings when reading files either on or created by a Macintosh computer, enabling the auto_detect_line_endings run-time configuration option may help resolve the problem. -- fgetscv

What platform are you on? Either way, check the line endings.

灯下孤影 2024-11-09 04:42:47

我修改了第一个答案以更好地处理不同的列分隔符

if (($handle = fopen("hdbsource/products_stock.csv", "r")) !== FALSE) {
    while (($csv[] = fgetcsv($handle, 1000, ',')) !== FALSE) {}
    fclose($handle);
}
$keys = array_shift($csv);

foreach ($csv as $i=>$row) {
    $csv[$i] = array_combine($keys, $row);
}

I modified first answer to better handle different column separators

if (($handle = fopen("hdbsource/products_stock.csv", "r")) !== FALSE) {
    while (($csv[] = fgetcsv($handle, 1000, ',')) !== FALSE) {}
    fclose($handle);
}
$keys = array_shift($csv);

foreach ($csv as $i=>$row) {
    $csv[$i] = array_combine($keys, $row);
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文