我尝试使用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,fonction,主机名,域名
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
)
)
答案 0 :(得分:0)
请尝试以下代码:
function csv_to_array($filename='', $delimiter=',')
{
if(!file_exists($filename) || !is_readable($filename))
return FALSE;
$header = NULL;
$data = array();
if (($handle = fopen($filename, 'r')) !== FALSE)
{
while (($row = fgetcsv($handle, 1000, $delimiter)) !== FALSE)
{
if(!$header)
$header = $row;
else
$data[] = array_combine($header, $row);
}
fclose($handle);
}
return $data;
}
您可以将此功能用作:
$tempArray = csv_to_array('myfile.csv');
然后一些简单的循环和操作函数结果如下将给出预期的输出:
$outArray = array();
foreach($tempArray as $temp)
foreach($temp as $key => $val)
$outArray[$key][] = $val;
echo "<pre>"; print_r($outArray);
结果将是:
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
)
)
<强> Function source 强>
答案 1 :(得分:0)
与Suyog的好答案不同的是:
if (($handle = fopen($target_file, "r")) !== FALSE) {
$size = filesize($target_file);
if(!$size) {
echo "Empty file.<br/>";
exit;
}
$header = false;
$content = array();
while ($row = fgetcsv($handle)) {
if (!$header) {
$header = $row;
continue;
}
// $row will be 0=>1, 1=>Elemental 6, 2=>MTLLXISNET06, 3=>local
// check if content has 0, 1, 2 and 3 keys
// If not, create them and assign value as an array
// If those key exist, push new value into the array
foreach ($row as $key=>$value) {
if (!array_key_exists($key, $content)) {
$content[$key] = array($value);
} else {
$temp = $content[$key];
array_push($temp, $value);
$content[$key] = $temp;
}
}
}
$final = array_combine($header, $content);
print_r($final);
fclose($handle);
}
结果:
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
)
)
测试示例:
$ cat test.txt
id,fonction,hostname,domain
1,Elemental 6,MTLLXISNET06,local
2,Elemental 7,MTLLXISNET07,local
3,Crawl Manager,RDICRAWLMANAGER,local
测试代码:
<?php
$handle = fopen('test.txt', 'r');
$header = false;
$content = array();
while ($row = fgetcsv($handle)) {
if (!$header) {
$header = $row;
continue;
}
foreach ($row as $key=>$value) {
if (!array_key_exists($key, $content)) {
$content[$key] = array($value);
} else {
$temp = $content[$key];
array_push($temp, $value);
$content[$key] = $temp;
}
}
}
$final = array_combine($header, $content);
print_r($final);
?>