39

我有以下 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,,

当我这样做时:

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

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

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

4

6 回答 6

107

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

ini_set('auto_detect_line_endings', true);

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

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

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

于 2011-04-04T16:18:28.880 回答
7

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

$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);

输出:

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] => 
        )

)
于 2011-04-04T14:39:57.310 回答
1

尝试:

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

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

于 2011-04-04T13:22:14.483 回答
1

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

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

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


非特定行尾的解决方法:

$data = array_map("str_getcsv", preg_split('/[\r\n]+/', file_get_contents($fn)));
var_dump($data);
于 2011-04-04T13:34:16.900 回答
1

在 Mac 上使用 Microsoft Excel 生成的 CSV 文件时遇到了同样的问题。我在不同的文件上执行了更多的命令,并且行尾似乎不同。

$ more excel.csv && more test.csv
aaa@aaa.fr^Mbbb@bbb.fr^Mccc@ccc.fr^M
ddd@ddd.fr
eee@eee.fr
fff@fff.fr

我用了

ini_set('auto_detect_line_endings', true);

fgetcsv($handle, 0, ';')

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

于 2017-03-10T10:50:28.630 回答
0

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

于 2012-08-29T01:20:30.770 回答