【发布时间】:2015-12-16 04:23:18
【问题描述】:
我尝试使用 CSV 文件创建一个数组并使用第一行作为键。在 PHP 中。
我的代码是:
if (($handle = fopen($target_file, "r")) !== FALSE) {
$size = filesize($target_file);
if(!$size) {
echo "Empty file.<br/>";
exit;
}
$csvcontent = array();
$header = null;
while ($row = fgetcsv($handle)) {
if($header === null) {
$header = $row;
continue;
}
$csvcontent[] = array_combine($header, $row);
}
fclose($handle);
}
最终的格式并不是我所需要的。
有人可以帮助我获得更好的结果吗?
如果 CSV 文件是这样的:
id、功能、主机名、域
1,元素6,MTLLXISNET06,本地
2,元素7,MTLLXISNET07,本地
3、爬网管理器、RDICRAWLMANAGER、本地
我需要这样的结果:
Array
(
[id] => Array
(
[0] => 1
[1] => 2
[2] => 3
)
[fonction] => Array
(
[0] => Elemental 6
[1] => Elemental 7
[2] => Crawl Manager
)
[hostname] => Array
(
[0] => MTLLXISNET06
[1] => MTLLXISNET07
[2] => RDICRAWLMANAGER
)
[domain] => Array
(
[0] => local
[1] => local
[2] => local
)
)
【问题讨论】: