php str_getcsv 数组问题
我正在上传 csv 文件,然后使用 str_getcsv 解析它。一切都很好,只是我需要一种方法来循环它们。理想情况下,如果数组返回并看起来像这样,那就太好了:
Array (
[1] => Array
(
[0] => 1 // first id in csv
[1] => name
[2] => address
[3] => town
[4] => state
[5] => zip
[6] => phone
[7] => website
[8] => other
[9] => other
)
[22] => Array
(
[10] => name
[11] => address
[12] => town
[13] => state
[14] => zip
[15] => phone
[16] => website
[17] => other
[18] => other
)
[24] => Array
(
[19] => name
[20] => address
[21] => town
[22] => state
[23] => zip
[24] => phone
[25] => website
[26] => other
[27] => other
)
)
但是数据返回如下:
Array
(
[0] => 1 // first id in csv
[1] => name
[2] => address
[3] => town
[4] => state
[5] => zip
[6] => phone
[7] => website
[8] => other
[9] => other
22 // id field
[10] => name
[11] => address
[12] => town
[13] => state
[14] => zip
[15] => phone
[16] => website
[17] => other
[18] => other
24// id field
[19] => name
[20] => address
[21] => town
[22] => state
[23] => zip
[24] => phone
[25] => website
[26] => other
[27] => other
关于如何修复此问题以使其看起来像顶部的数组有什么建议吗? 现在我正在做:
$csvfile = file_get_contents($targetFile);
$csv = str_getcsv($csvfile);
I am uploading a csv file and then parsing it using str_getcsv. All works great except that I need a way to cycle through them. Ideally, it'd be great to have the array come back and look like this:
Array (
[1] => Array
(
[0] => 1 // first id in csv
[1] => name
[2] => address
[3] => town
[4] => state
[5] => zip
[6] => phone
[7] => website
[8] => other
[9] => other
)
[22] => Array
(
[10] => name
[11] => address
[12] => town
[13] => state
[14] => zip
[15] => phone
[16] => website
[17] => other
[18] => other
)
[24] => Array
(
[19] => name
[20] => address
[21] => town
[22] => state
[23] => zip
[24] => phone
[25] => website
[26] => other
[27] => other
)
)
However the data comes back like the following:
Array
(
[0] => 1 // first id in csv
[1] => name
[2] => address
[3] => town
[4] => state
[5] => zip
[6] => phone
[7] => website
[8] => other
[9] => other
22 // id field
[10] => name
[11] => address
[12] => town
[13] => state
[14] => zip
[15] => phone
[16] => website
[17] => other
[18] => other
24// id field
[19] => name
[20] => address
[21] => town
[22] => state
[23] => zip
[24] => phone
[25] => website
[26] => other
[27] => other
Any suggestions on how to fix this to look like the array at the top?
right now I'm doing:
$csvfile = file_get_contents($targetFile);
$csv = str_getcsv($csvfile);
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
str_getcsv() 期望作为参数传递的字符串为一条记录。
但由于您的源是一个文件,无论如何,最简单的方法可能是使用 fgetcsv() 而不是 str_getcsv()
独立示例:
打印
edit2: 使用每个记录的第一个元素作为结果数组中的键:
str_getcsv() expects the string passed as parameter to be one record.
But since your source is a file anyway the easiest way is probably to use fgetcsv() instead of str_getcsv()
self-contained example:
prints
edit2: For using the first element of each record as the key in the result array:
这不是这个世界上最美丽的事情,但我认为它有效。
代码的结果:
希望这有帮助
Not the pretties thing on this world but I think it works.
result of the code:
Hope this helps
好吧,只需计算一个条目的项目数,然后在一个循环中进行一个循环,
这就是我刚刚所做的并且效果很好。
Well, just count the number of items an entry has then make a loop inside a loop
That is what I just did and works very well.