php fgetcsv 返回所有行

发布于 2024-10-30 11:41:47 字数 1256 浏览 1 评论 0原文

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

I have the following csv file:

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.

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

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

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

发布评论

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

评论(6

∞梦里开花 2024-11-06 11:41:47

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

ini_set('auto_detect_line_endings', true);

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

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

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

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

ini_set('auto_detect_line_endings', true);

If that doesn't resolve the issue, then detect the type of line terminators using the file command:

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

You can then convert the 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.

雨后彩虹 2024-11-06 11:41:47

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

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

)

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

)
因为看清所以看轻 2024-11-06 11:41:47

尝试:

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

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

try:

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

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

别在捏我脸啦 2024-11-06 11:41:47

要立即读入整个 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);

To read in the whole CSV file at once use:

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


The workaround for unspecific line endings:

$data = array_map("str_getcsv", preg_split('/[\r\n]+/', file_get_contents($fn)));
var_dump($data);
独享拥抱 2024-11-06 11:41:47

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

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

ini_set('auto_detect_line_endings', true);

以前

fgetcsv($handle, 0, ';')

用过并且有效。接受的答案是正确的。

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

$ 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.

将军与妓 2024-11-06 11:41:47

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

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文