<?php
/**
* 从两个.csv 文件中读出数据
* 比较这两个文件不同的数据,并存入.csv 文件中
*/
header("Content-type:text/html;charset=utf-8");
class Readfiledata {
/**
* 链接数据库
*/
private static function connect(){
require_once 'index2.php';
mysql_connect('localhost','root','');
mysql_select_db('sinapay');
}
/**
* 读文件并获取数据
*/
private static function getdata($file) {
$handle = fopen ( $file, 'r' );
$orderform = array ();
$i=0;
while ( false != ($data = fgetcsv ($handle,0,',')) ) {
$i++;
if($i==1) continue;
$orderform [] =trim($data [0],"\t\n\r\0\x0B`\'");
}
fclose ( $handle );
return $orderform;
}
/**
* 获取两个文件不同的数据
*
* @param String $file1
* @param String $file2
*/
private static function getdiffdata($file1, $file2) {
$orderform = self::getdata ( $file1 );
$orderform2 = self::getdata ( $file2 );
$diff1 = array_diff ($orderform,$orderform2 );
$diff2 = array_diff ($orderform2,$orderform );
$todiff = array_merge ( $diff1, $diff2 );
$todif=array_values(array_unique($todiff));
return $todif;
}
/**
* 数据写入.csv 文件中
* @param String $filename
* @param String $file1
* @param String $file2
*/
private static function writefile($filename, $file1, $file2) {
$todiffdata = self::getdiffdata ( $file1, $file2 );
$newarray=array();
self::connect();
if(empty($todiffdata)){
echo "两个文件的数据一致";
die;
}
$toparray=array("charge_id","bussined_id");
$condition='';
$counarray=count($todiffdata)-1;
foreach($todiffdata as $key=>$val){
if($key==$counarray){
$condition.="charge_id=$val";
}else{
$condition.="charge_id=$val or ";
}
}
$sql="select charge_id,business_id from sinapay_charge_final where $condition";
$result=mysql_query($sql);
while($res=mysql_fetch_array($result)){
$data=array('charge_id'=>$res['charge_id'],'business_id'=>$res['business_id']);
$newarray[]=$data;
}
if (! is_file ( $filename)) {
touch ( $filename);
}
$handle = fopen ($filename, 'a' );
fputcsv($handle, $toparray);
foreach($newarray as $value){
fputcsv ( $handle, $value );
}
fclose ( $handle );
}
/**
* 入口文件
*/
public static function main($filename,$file1,$file2) {
self::writefile ( $filename, $file1, $file2 );
}
}
$filename = 'total.csv';
$file1 = 'ac.csv';
$file2 = 'ad.csv';
Readfiledata::main ($filename, $file1, $file2 );
读取两文件,不同的内容存入另一个文件中
最新推荐文章于 2024-07-24 10:09:39 发布