tag: OS file data Io for art Re C
<? PHP/*** read data from two. CSV files * compare the data of these two files and save it to The. CSV file */class readfiledata {private function _ construct () {}/*** read the file and obtain data */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 ;} /*** get different data from two files ** @ 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_d IFF ($ orderform2, $ orderform); $ todiff = array_merge ($ diff1, $ diff2); $ todif = array_unique ($ todiff); return $ todif ;} /*** write data into the. CSV file ** @ 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); $ newa Rray [] = $ 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);}/*** entry file */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 );