PHP fgetcsv返回所有行

我有以下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); 我得到了同样的结果

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

 ini_set('auto_detect_line_endings', true); 

如果这样不能解决问题,则使用file命令检测行结束符的types:

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

然后你可以转换行结束符。 我不知道你使用的是什么操作系统,但是有很多的文件格式转换工具 ,例如dos2unix

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

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

尝试:

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

另外,你可以尝试使用str_getcsv(file_get_contents(...))

在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, ';') 

它的工作。 接受的答案是正确的。

要一次读取整个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); 

如果你有select,回去编辑你的.csv文件以包含“Unix”风格的行结尾…然后fgetcsv会自动按行结束