php fgetcsv返回所有行

时间:2011-04-04 13:19:21

标签: 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);,我得到了相同的结果

6 个答案:

答案 0 :(得分:102)

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

ini_set('auto_detect_line_endings', true);

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

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

然后,您可以转换行结尾。我不确定您使用的是哪种操作系统,但有很多utilities out there for file format conversion,例如dos2unix

答案 1 :(得分:4)

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

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

)

答案 2 :(得分:1)

尝试:

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

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

答案 3 :(得分:1)

在Mac上使用Microsoft Excel生成的CSV文件遇到了同样的问题。 我在不同的文件上执行了 more 命令,结果显示行结尾不同。

ini_set('auto_detect_line_endings', true);

我用过

fgetcsv($handle, 0, ';')

id

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

答案 4 :(得分: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);

答案 5 :(得分:0)

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