Read two files and store different contents into another file.

Source: Internet
Author: User

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 );

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.