我正在尝试使用PHP构建(编程)接口,该接口采用1个CSV文件(csv1)并使用该CSV文件中的标头来映射另一个CSV文件(csv2)的列。我能够使用以下内容将映射映射到数组中:
//csv1
$data = array_map('str_getcsv', file('csv1.csv'));
//just get the first line of the csv1's values
$csv1Headers = array_shift($data);
//csv2
$data2 = array_map('str_getcsv', file('csv2.csv'));
//get csv2's header values
$csv2Headers = array_shift($data2);
//set variables based on csv2's headers
$csv2header1 = array_search("header1", $csv2Headers);
$csv2header2 = array_search("header2", $csv2Headers);
$csv2header3 = array_search("header3", $csv2Headers);
$csv2header4 = array_search("header4", $csv2Headers);
//create main array
$mappedArray = array();
//create individual arrays based on csv1's header values
foreach($csv1Headers as $header) {
$mappedArray[$header] = array();
}
//Set the
foreach ($data2 as $row) {
if($row[$csv2header1]) { $mappedArray['csv1header1'][] .= $row[$csv2header1]; } else { $mappedArray['csv1header1'][] .= ''; }
if($row[$csv2header2]) { $mappedArray['csv1header2'][] .= $row[$csv2header2]; } else { $mappedArray['csv1header2'][] .= ''; }
if($row[$csv2header3]) { $mappedArray['csv1header3'][] .= $row[$csv2header3]; } else { $mappedArray['csv1header3'][] .= ''; }
if($row[$csv2header4]) { $mappedArray['csv1header4'][] .= $row[$csv2header4]; } else { $mappedArray['csv1header4'][] .= ''; }
}
如果我运行print_r($mappedArray)
,我会得到以下输出:
Array
(
[csv1Header1] => Array
(
[0] => csv2DataMappedToHeader1
[1] => csv2DataMappedToHeader1
[2] => csv2DataMappedToHeader1
)
[csv1Header2] => Array
(
[0] => csv2DataMappedToHeader2
[1] => csv2DataMappedToHeader2
[2] => csv2DataMappedToHeader2
)
[csv1Header3] => Array
(
[0] => csv2DataMappedToHeader3
[1] => csv2DataMappedToHeader3
[2] => csv2DataMappedToHeader3
)
[csv1Header4] => Array
(
[0] => csv2DataMappedToHeader4
[1] => csv2DataMappedToHeader4
[2] => csv2DataMappedToHeader4
)
)
现在我想知道的是如何编写一个输出的新CSV文件:
"csv1Header1", "csv1Header2", "csv1Header3", "csv1Header4"
"csv2DataMappedToHeader1","csv2DataMappedToHeader2","csv2DataMappedToHeader3","csv2DataMappedToHeader4"
"csv2DataMappedToHeader1","csv2DataMappedToHeader2","csv2DataMappedToHeader3","csv2DataMappedToHeader4"
"csv2DataMappedToHeader1","csv2DataMappedToHeader2","csv2DataMappedToHeader3","csv2DataMappedToHeader4"
我无法弄清楚如何做到这一点......任何建议?
答案 0 :(得分:0)
您可以使用fputcsv
将输出写入文件:
$array = ('apple', 'banana', 'carrot', 'date');
$fh = fopen('output.csv', 'w');
fputcsv($fh, $array);
output.csv
:
"apple","banana","carrot","date"
但是,您可能会发现您使用的数据结构并非都有利于打印,因为它是按行而不是行排列的。
作为一般提示,每当您发现自己重复操作两次以上时,值得抽象操作以创建更通用的版本。例如,此代码:
//set variables based on csv2's headers
$csv2header1 = array_search("header1", $csv2Headers);
$csv2header2 = array_search("header2", $csv2Headers);
$csv2header3 = array_search("header3", $csv2Headers);
$csv2header4 = array_search("header4", $csv2Headers);
可以重构;而不是使用一组具有相似名称的变量,使用for
循环并将数据放入数组中:
$csv2hdr = array();
for ($i=1; $i<5; $i++) {
# searches for "header1", "header2", etc.
# stores them in $csv2hdr[1], $csv2hdr[2], etc.
$csv2hdr[$i] = array_search("header$i", $csv2Headers);
}
当你在这里使用.=
运算符时,看起来你正在使用字符串追加运算符(=
):
$mappedArray['csv1header1'][] .= $row[$csv2header1];
希望有所帮助!