如何在PHP5.6中使用头文件创建带有CSV文件的数组?
如何在PHP5.6中使用头文件创建带有CSV文件的数组?,php,csv,Php,Csv,我尝试用CSV文件创建一个数组,并使用第一行作为键。在PHP中。 我的代码是: if (($handle = fopen($target_file, "r")) !== FALSE) { $size = filesize($target_file); if(!$size) { echo "Empty file.<br/>"; exit; } $csvcontent = array(); $header = nul
我尝试用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);
}
请尝试以下代码:
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);
与Suyog的好答案不同的方法是:
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
)
)
测试示例:
<?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);
?>
测试代码:
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);
?>