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);
并且得到了相同的结果
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
这很可能是行结束问题。尝试启用
auto_detect_line_endings
< /a> 将尝试确定文件的行结尾。如果这不能解决问题,则使用 检测行终止符的类型“noreferrer">
file
命令:然后您可以转换行结尾。我不确定您使用的是什么操作系统,但是有很多用于文件格式转换的实用程序< /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.If that doesn't resolve the issue, then detect the type of line terminators using the
file
command: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
.这是我能想到的最短的解决方案:
输出:
This is the shortest solution I could think of:
Output:
尝试:
或者,您可以尝试使用
str_getcsv(file_get_contents(...))
try:
Alternatively, you could try using
str_getcsv(file_get_contents(...))
要立即读入整个 CSV 文件,请使用:
尽管您必须对第一行进行 array_shift() (如果您不使用列键)。
不特定行结尾的解决方法:
To read in the whole CSV file at once use:
Though you will have to array_shift() the first row (if you don't use the column keys).
The workaround for unspecific line endings:
在 Mac 上,使用 Microsoft Excel 生成的 CSV 文件也遇到同样的问题。
我在不同的文件上执行了命令 more,并且行结尾似乎有所不同。
我
以前
用过并且有效。接受的答案是正确的。
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.
I used
before
and it works. The accepted answer is correct.
如果您可以选择,返回编辑您的 .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