用php合并两个csv文件

时间:2015-08-05 16:41:43

标签: php csv

我有两个csv文件:一个是合同数据,另一个是合同。我需要使用公共字段(合同名称)组合这两个文件,并计算当前授予合同的总金额。 链接到csv文件: https://github.com/younginnovations/problem-statements/tree/master/clean-up-contracts-data

我确实得到了所需的输出bt未定义的偏移错误出现了。      代码:unset(awards_info [$ y] [0]);取消设置数据,但发生'未定义的偏移量:0'错误。  类似地,代码$ list_data [10]!= NULL也显示相同的错误 但两者都产生了理想的产出。

var_dump($awards_info):: produces following:
array(5) { [0]=> array(6) { [0]=> string(12) "contractName" [1]=> string(12) "contractDate" [2]=> string(14) "completionDate" [3]=> string(7) "awardee" [4]=> string(15) "awardeeLocation" [5]=> string(6) "Amount" } [1]=> array(6) { [0]=> string(15) "Contract-2070-3" [1]=> string(8) "5/9/2014" [2]=> string(9) "8/25/2014" [3]=> string(11) "SK Builders" [4]=> string(5) "Banke" [5]=> string(6) "200000" } [2]=> array(6) { [0]=> string(15) "Contract-2070-5" [1]=> string(9) "3/18/2014" [2]=> string(8) "4/8/2014" [3]=> string(24) "S engineering industries" [4]=> string(9) "Makwanpur" [5]=> string(6) "300000" } [3]=> array(6) { [0]=> string(15) "Contract-2070-9" [1]=> string(8) "3/6/2014" [2]=> string(8) "4/6/2014" [3]=> string(24) "Gourishankar nirman sewa" [4]=> string(8) "Lalitpur" [5]=> string(6) "400000" } [4]=> array(6) { [0]=> string(16) "Contract-2070-10" [1]=> string(8) "2/6/2014" [2]=> string(9) "6/16/2014" [3]=> string(11) "SK Builders" [4]=> string(5) "Banke" [5]=> string(6) "400000" } }
$file_contract=fopen('contracts.csv','r');
$file_awards=fopen('awards.csv','r');
 while(($data=fgetcsv($file_contract))!=FALSE){
$contracts_info[]=$data;
}

while(($data=fgetcsv($file_awards))!=FALSE){
$awards_info[]=$data;
}

$con_count=count($contracts_info);
for($x=0;$x<$con_count;$x++){

    if($x==0){
        unset($awards_info[0][0]);
        $data[$x]=array_merge($contracts_info[0],$awards_info[0]);
        }

    else{
        $check=0;
        $award_count=count($awards_info);
        for($y=1;$y<$award_count;$y++){

            if($awards_info[$y][0]==$contracts_info[$x][0]){

                unset($awards_info[$y][0]);
                $data[$x]=array_merge($contracts_info[$x],$awards_info[$y]);
                $check=1;
                }

            }

            if($check==0){
                $data[$x]=$contracts_info[$x];
                }
        }
}
$final_data=fopen('final.csv','w');
foreach($data as $list_data){
    fputcsv($final_data,$list_data);
    }
    fclose($final_data);
    $c=0;


foreach ($data as $list_data){
    if(($list_data[1]=='Current') && ($list_data[10]!=NULL))
    {
        $c+=$list_data[12];

        }
    }

echo $c;

1 个答案:

答案 0 :(得分:0)

您只需在新文件中添加内容文件,而无需解析文件:

$fp = fopen('final.csv','w');
fwrite($fp, file_get_contents('contracts.csv'));
fwrite($fp, file_get_contents('awards.csv'));
fclose($fp);
echo file_get_contents('final.csv');
好吧,这是错误的方式。所以我尝试重写循环:

for($x=0; $x < count($contracts_info); $x++) {
  $data[$x] = $contracts_info[$x];
  if($x == 0) {
    if(isset($awards_info[$x])) {
      $data[$x] = array_merge($data[$x], $awards_info[$x]);
      unset($awards_info[$x]);
    }
  } else {
    foreach($awards_info as $y => $award_info_datas) {
      if($award_info_datas[0] == $contracts_info[$x][0]) {
        $data[$x] = array_merge($data[$x], $award_info_datas);
        unset($awards_info[$y]);
        break;
      }
    }
  }
}