从csv中获取关联数组
我从 url 打开一个 csv 文件。每行有 4 个字段,每个字段都有一个名称:
Field1;Field2;Field3;Field4
现在我的脚本将 csv 数据作为一行处理,但我想这样处理:
Array
(
[0] => array(
['field1'] => 1
['field2'] => 2
['field3'] => 3
['field4'] => 4
)
)
有什么想法吗?
这是我的代码:
if (($handle = fopen ( $eurl, "r" )) !== FALSE) {
while ( ($data = fgetcsv ( $handle, 4096, ";" )) !== FALSE ) {
$num = count ( $data );
for($c = 0; $c < $num; $c ++) {
echo $data [$c];
}
}
fclose ( $handle );
}
I open a csv file from a url. Each line has 4 fields and each field has a name:
Field1;Field2;Field3;Field4
Now my script handles the csv data as one single line but I want to have it this way:
Array
(
[0] => array(
['field1'] => 1
['field2'] => 2
['field3'] => 3
['field4'] => 4
)
)
Any ideas?
Here is my code:
if (($handle = fopen ( $eurl, "r" )) !== FALSE) {
while ( ($data = fgetcsv ( $handle, 4096, ";" )) !== FALSE ) {
$num = count ( $data );
for($c = 0; $c < $num; $c ++) {
echo $data [$c];
}
}
fclose ( $handle );
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
t.csv
身份证号;姓名;性别;年龄
1;旭东;m;23
2;杰克;f;24
3;敏杰;f;25
t.csv
id;name;sex;age
1;xudong;m;23
2;jack;f;24
3;minjie;f;25