我有两套csv文件 - 一组包含合同数据,另一组包含授予的合同。我需要使用公共字段(contractName)合并两个csv文件,并计算已结算的合同总额。Link to the csv files
到目前为止,我已经设法将两个csv文件合并在一起并将其写入final.csv文件中,但我无法使用公共字段(contractName)合并两个csv文件。这是代码。
<?php
$nn = 0;
foreach (glob("*.csv") as $filename) {
if (($handle = fopen($filename, "r")) !== FALSE) {
while (($data = fgetcsv($handle, 0, ",")) !== FALSE) {
$c = count($data);
for ($x=0;$x<$c;$x++)
{
$csvarray[$nn][] = $data[$x];
}
$nn++;
}
fclose($handle);
}
}
$fp = fopen('../final.csv', 'w');//output file set here
foreach ($csvarray as $fields) {
fputcsv($fp, $fields);
}
fclose($fp);?>
这是我的最终输出。
contractName,contractDate,completionDate,awardee,awardeeLocation,Amount
Contract-2070-3,5/9/14,8/25/14,"SK Builders",Banke,200000
Contract-2070-5,3/18/14,4/8/14,"S engineering industries",Makwanpur,300000
Contract-2070-9,3/6/14,4/6/14,"Gourishankar nirman sewa",Lalitpur,400000
Contract-2070-10,2/6/14,6/16/14,"SK Builders",Banke,500000
contractname,status,bidPurchaseDeadline,bidSubmissionDeadline,bidOpeningDate,tenderid,publicationDate,publishedIn
Contract-2070-1,Closed,6/12/14,6/13/14,6/13/14,2070/071/2,5/14/14,"Nagarik Daily"
Contract-2070-2,Closed,6/10/14,6/11/14,6/11/14,16/070/71,5/12/14,"The Himalayan Times"
Contract-2070-3,Current,3/8/14,3/9/14,3/9/14,DDC/Bag/Bridge/03-070/71,3/10/14,"Nagarik Daily"
Contract-2070-4,Current,4/23/14,4/25/14,4/25/14,04(2070/071),4/9/14,"Hetauda sandesh"
Contract-2070-5,Closed,4/23/14,4/25/14,4/26/14,04(2070/071),4/10/14,"Hetauda sandesh"
Contract-2070-6,Current,4/23/14,4/25/14,4/27/14,04(2070/071),4/11/14,"Hetauda sandesh"
Contract-2070-7,Current,4/23/14,4/25/14,4/28/14,04(2070/071),4/12/14,"Hetauda sandesh"
Contract-2070-8,Current,4/23/14,4/25/14,4/29/14,04(2070/071),4/13/14,"Hetauda sandesh"
Contract-2070-9,Closed,2/6/14,2/8/14,2/8/14,15/070/71,1/9/14,"The Himalayan Times"
Contract-2070-10,Current,1/14/14,1/15/14,1/16/14,"13,2070/2071",1/6/14,"The Himalayan Times"
但最终输出应该是这样的。
contractname,status,bidPurchaseDeadline,bidSubmissionDeadline,bidOpeningDate,tenderid,publicationDate,publishedIn,contractDate,completionDate,awardee,awardeeLocation,Amount
Contract-2070-1,Closed,6/12/14,6/13/14,6/13/14,2070/071/2,5/14/14,Nagarik Daily,,,,,
Contract-2070-2,Closed,6/10/14,6/11/14,6/11/14,16/070/71,5/12/14,The Himalayan Times,,,,,
Contract-2070-3,Current,3/8/14,3/9/14,3/9/14,DDC/Bag/Bridge/03-070/71,3/10/14,Nagarik Daily,5/9/14,8/25/14,SK Builders,Banke,200000
Contract-2070-4,Current,4/23/14,4/25/14,4/25/14,04(2070/071),4/9/14,Hetauda sandesh,,,,,
Contract-2070-5,Closed,4/23/14,4/25/14,4/26/14,04(2070/071),4/10/14,Hetauda sandesh,3/18/14,4/8/14,S engineering industries,Makwanpur,300000
Contract-2070-6,Current,4/23/14,4/25/14,4/27/14,04(2070/071),4/11/14,Hetauda sandesh,,,,,
Contract-2070-7,Current,4/23/14,4/25/14,4/28/14,04(2070/071),4/12/14,Hetauda sandesh,,,,,
Contract-2070-8,Current,4/23/14,4/25/14,4/29/14,04(2070/071),4/13/14,Hetauda sandesh,,,,,
Contract-2070-9,Closed,2/6/14,2/8/14,2/8/14,15/070/71,1/9/14,The Himalayan Times,3/6/14,4/6/14,Gourishankar nirman sewa,Lalitpur,400000
Contract-2070-10,Current,1/14/14,1/15/14,1/16/14,"13, 2070/2071",1/6/14,The Himalayan Times,2/6/14,6/16/14,SK Builders,Banke,500000
答案 0 :(得分:1)
这个问题不是很难,你总是可以把数据放在数组的csv上并使用它们,就像这个解决方案一样:
// 1st section
$fh = fopen('awards.csv', 'r');
$fhg = fopen('contracts.csv', 'r');
while (($data = fgetcsv($fh, 0, ",")) !== FALSE) {
$awards[]=$data;
}
while (($data = fgetcsv($fhg, 0, ",")) !== FALSE) {
$contracts[]=$data;
}
// 2nd section
for($x=0;$x< count($contracts);$x++)
{
if($x==0){
unset($awards[0][0]);
$line[$x]=array_merge($contracts[0],$awards[0]); //header
}
else{
$deadlook=0;
for($y=0;$y <= count($awards);$y++)
{
if($awards[$y][0] == $contracts[$x][0]){
unset($awards[$y][0]);
$line[$x]=array_merge($contracts[$x],$awards[$y]);
$deadlook=1;
}
}
if($deadlook==0)
$line[$x]=$contracts[$x];
}
}
// 3 section
$fp = fopen('final.csv', 'w');//output file set here
foreach ($line as $fields) {
fputcsv($fp, $fields);
}
fclose($fp);
我很难解释这段代码,因为来自一个西班牙国家,所以我的英语不是很好......但我可以试试
基本上代码有3个部分,
在第1节中, 打开这两个文件并将内容放入数组$ awards []和$ contracts []中,因此$ awards [0]是awards.csv中的第一行,$ awards [1]是awards.csv中的第二行,等,在$ contracts []中也一样。
在第2部分,
我比较每个数组中的第一个单词,$ awards [x] [0]和$ contracts [x] [0]。
第一个if
,if($ x == 0),是要制作标题。
首先,我使用contractname
函数删除第一个单词unset
,并使用array_merge
函数加入$ awards [0]和$ contracts [0]。
然后,使用那些for
我从$ contracts数组的每一行中选择第一个单词,并与$ awards数组中每一行的第一个单词进行比较。
所以,if($awards[$y][0] == $contracts[$x][0])
检查那些第一个单词(ej。Contract-2070-3are
)是否相同,如果它们是相同的字符串,删除它并合并这些行。
如果这些单词不相同,请在$ line数组中保存$ contracts [x]行并继续。
在第3节中, 保存文件中$ line数组的内容。
答案 1 :(得分:0)
这是某种面试问题吗?您是否需要显示编写算法的能力或以实际方式解决问题?
对于大数据集,我可能只是将csv转储到sqlite数据库,每个csv一个表,并将它们与查询连接起来。
或者你可以用每个csv,$ contracts和$ awards填充两个关联数组,使用连接字段(contractName)作为两个数组的关键字。
然后你打破键,并用给定键的每个数组的内容填充$ final数组:
$final = array();
$keys = array_keys($contracts);
foreach($keys as $key) {
$final[] = array_merge($contracts[$key], $awards[$key]);
}