<?php
echo "</h4>MERGE STOCKS</h4>";
unset($arr);
$arr=Array();
$arr2=Array();
$handle=opendir("./RESULT");
while (($file = readdir($handle))!==FALSE) {
if(strtolower(substr($file,-4))==".csv") {
$fp=fopen("./RESULT/".$file,"rt");
while(!feof($fp)) {
$st=trim(fgets($fp));
if($st!="") {
$t=explode("|",$st);
if(@$t[0]!="") {
$idx=$t[0];
$arr[$idx]=@$arr[$idx]+(int)@$t[2];
$arr2[$idx]=@$arr2[$idx]."|".(int)@$t[2];
}

}
}
fclose($fp);
}
}
closedir($handle);
$fp=fopen("./RESULT/stock.csv","w");
foreach($arr as $key => $val) {
fputs($fp,$key."|".$val.@$arr2[$key]."\n");
}
fclose($fp);
?>