<?php /** * 从两个.csv 文件中读出数据 * 比较这两个文件不同的数据,并存入.csv 文件中 */ class Readfiledata { private function __construct() { } /** * 读文件并获取数据 */ 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],'`'); } 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_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 ); $partdata=array(); foreach ($todiffdata as $val){ $partdata=array($val); $newarray[]=$partdata; } if (! is_file ( "/tmp/$filename" )) { // unlink( "/tmp/$filename"); touch ( "/tmp/$filename"); } $handle = fopen ( "/tmp/$filename", 'w' ); 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 );
读取两文件,不同的内容存入另一个文件中,布布扣,bubuko.com
原文地址:http://blog.csdn.net/xingjigongsi/article/details/38099743