sta*_*ker 5 php csv parsing fgetcsv
我知道有很多资源可以将CSV放入关联数组中,但我找不到任何能帮助像我这样的菜鸟做我想做的事情.
我目前在PHP文件中定义了一个关联数组:
$users = array(
'v4f25' => 'Stan Parker',
'ntl35' => 'John Smith',
);
Run Code Online (Sandbox Code Playgroud)
我想将该数组移动到CSV文件(users.txt)中,以便:
v4f25, Stan Parker
ntl35, John Smith
Run Code Online (Sandbox Code Playgroud)
下一步是导入users.txt,这样我就可以像使用数组$ users一样使用它.
这里有什么帮助?我试过的最后一个代码返回了这个:(这不是我想要的)
array(2) {
["v4f25"]=>
string(5) "ntl35"
["Stan Parker"]=>
string(10) "John Smith"
}
Run Code Online (Sandbox Code Playgroud)
以下怎么样?
$data = array();
if ($fp = fopen('csvfile.csv', 'r')) {
while (!feof($fp)) {
$row = fgetcsv($fp);
$data[$row[0]] = $row[1];
}
fclose($fp);
}
Run Code Online (Sandbox Code Playgroud)