php fgetcsv 返回所有行

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5539053/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 21:45:41  来源:igfitidea点击:

php fgetcsv returning all lines

phpfgetcsv

提问by Chris Muench

I have the following csv file:

我有以下 csv 文件:

upc/ean/isbn,item name,category,supplier id,cost price,unit price,tax 1 name,tax 1 percent,tax 2 name,tax 2 percent,quantity,reorder level,description,allow alt. description,item has serial number
,Apple iMac,Computers,,10,12,8,8,10,10,10,1,Best computer,,
,Apple iMac,Computers,,10,12,8,8,10,10,10,1,Best computer,,
,Apple iMac,Computers,,10,12,8,8,10,10,10,1,Best computer,,
,Apple iMac,Computers,,10,12,8,8,10,10,10,1,Best computer,,
,Apple iMac,Computers,,10,12,8,8,10,10,10,1,Best computer,,
,Apple iMac,Computers,,10,12,8,8,10,10,10,1,Best computer,,
,Apple iMac,Computers,,10,12,8,8,10,10,10,1,Best computer,,
,Apple iMac,Computers,,10,12,8,8,10,10,10,1,Best computer,,
,Apple iMac,Computers,,10,12,8,8,10,10,10,1,Best computer,,
,Apple iMac,Computers,,10,12,8,8,10,10,10,1,Best computer,,
,Apple iMac,Computers,,10,12,8,8,10,10,10,1,Best computer,,
,Apple iMac,Computers,,10,12,8,8,10,10,10,1,Best computer,,

When I do this:

当我这样做时:

if (($handle = fopen($_FILES['file_path']['tmp_name'], "r")) !== FALSE) 
{
    $data = fgetcsv($handle);
    var_dump($data);
}

I get an array with 183 elements, I would expect to only get one line of the csv, but I get the whole file.

我得到一个包含 183 个元素的数组,我希望只得到一行 csv,但我得到了整个文件。

I even tried $data = fgetcsv($handle, 1000);and I got the same result

我什至尝试过$data = fgetcsv($handle, 1000);,但得到了相同的结果

回答by mholzmann

This is most likely a line ending issue. Try enabling auto_detect_line_endingswhich will attempt to determine the file's line endings.

这很可能是行结束问题。尝试启用auto_detect_line_endings这将尝试确定文件的行尾。

ini_set('auto_detect_line_endings', true);

If that doesn't resolve the issue, then detectthe type of line terminators using the filecommand:

如果这不能解决问题,则使用以下命令检测行终止符的类型file

$ file example.csv
example.csv: ASCII text, with CR line terminators

You can then convertthe line endings. I am not sure what OS you are using but there are a lot of utilities out there for file format conversion, e.g. dos2unix.

然后,您可以转换行尾。我不确定您使用的是什么操作系统,但是有很多用于文件格式转换实用程序,例如dos2unix.

回答by Czechnology

This is the shortest solution I could think of:

这是我能想到的最短的解决方案:

$fp = fopen('test.csv', 'r');

// get the first (header) line
$header = fgetcsv($fp);

// get the rest of the rows
$data = array();
while ($row = fgetcsv($fp)) {
  $arr = array();
  foreach ($header as $i => $col)
    $arr[$col] = $row[$i];
  $data[] = $arr;
}

print_r($data);

Output:

输出:

Array
(
    [0] => Array
        (
            [upc/ean/isbn] => 
            [item name] => Apple iMac
            [category] => Computers
            [supplier id] => 
            [cost price] => 10
            [unit price] => 12
            [tax 1 name] => 8
            [tax 1 percent] => 8
            [tax 2 name] => 10
            [tax 2 percent] => 10
            [quantity] => 10
            [reorder level] => 1
            [description] => Best computer
            [allow alt. description] => 
            [item has serial number] => 
        )

    [1] => Array
        (
            [upc/ean/isbn] => 
            [item name] => Apple iMac
            [category] => Computers
            [supplier id] => 
            [cost price] => 10
            [unit price] => 12
            [tax 1 name] => 8
            [tax 1 percent] => 8
            [tax 2 name] => 10
            [tax 2 percent] => 10
            [quantity] => 10
            [reorder level] => 1
            [description] => Best computer
            [allow alt. description] => 
            [item has serial number] => 
        )

    // ...

    [11] => Array
        (
            [upc/ean/isbn] => 
            [item name] => Apple iMac
            [category] => Computers
            [supplier id] => 
            [cost price] => 10
            [unit price] => 12
            [tax 1 name] => 8
            [tax 1 percent] => 8
            [tax 2 name] => 10
            [tax 2 percent] => 10
            [quantity] => 10
            [reorder level] => 1
            [description] => Best computer
            [allow alt. description] => 
            [item has serial number] => 
        )

)

回答by Arnaud Schneider

Got the same problem, on a Mac, with a CSV file generated by Microsoft Excel. I executed the command moreon different files, and it appears that the line ending differ.

在 Mac 上使用 Microsoft Excel 生成的 CSV 文件遇到了同样的问题。我在不同的文件上执行了更多的命令,看起来行尾不同。

$ more excel.csv && more test.csv
[email protected]^[email protected]^[email protected]^M
[email protected]
[email protected]
[email protected]

I used

我用了

ini_set('auto_detect_line_endings', true);

before

fgetcsv($handle, 0, ';')

and it works. The accepted answer is correct.

它有效。接受的答案是正确的。

回答by Martin

try:

尝试:

 while (($data = fgetcsv($handle, 0, ',')) !== FALSE) {
    $columns = count($data);
 }

Alternatively, you could try using str_getcsv(file_get_contents(...))

或者,您可以尝试使用 str_getcsv(file_get_contents(...))

回答by mario

To read in the whole CSV file at once use:

要一次读取整个 CSV 文件,请使用:

$data = array_map("str_getcsv", file($fn));
var_dump($data);

Though you will have to array_shift() the first row (if you don't use the column keys).

尽管您必须将 array_shift() 放在第一行(如果您不使用列键)。



The workaround for unspecific line endings:

非特定行结尾的解决方法:

$data = array_map("str_getcsv", preg_split('/[\r\n]+/', file_get_contents($fn)));
var_dump($data);

回答by Dr Nick Engerer

If you have the option, go back an edit your .csv file to include "Unix" style line endings...then fgetcsv will automatically break the array up by line endings

如果您有选择,请返回编辑您的 .csv 文件以包含“Unix”样式的行尾...然后 fgetcsv 将自动按行尾拆分数组