1

I have two csv files, and both have same data structure.

ID - Join_date - Last_Login

I want to compare and get the exactly matching records numbers based on this example:

the first files has 100 records, of which 20 are not included in the 2nd file.

the 2nd file has 120 records.

I want a script in PHP to compare these two files and build two separate CSV files.

And I want to remove all extra records from the 2nd file which are not included in the first file.
And remove all records from the first file which are not included in the 2nd file.

Thanks

Community
  • 1
  • 1
sevoug
  • 149
  • 1
  • 1
  • 12
  • @TheJolly: [Please remove "thanks" and similar when editing](http://meta.stackexchange.com/questions/2950/should-hi-thanks-taglines-and-salutations-be-removed-from-posts). Thanks! – sarnold Apr 04 '12 at 22:57

2 Answers2

0

There is a GNU utility comm that will do this really easily. You could exec that through php or just do it directly. If you don't have access to comm, the easiest thing to do would be to store both files in an array (probably via file()) and use array_intersect().

Explosion Pills
  • 176,581
  • 46
  • 285
  • 363
0

You an try this for limited number of CSV file .. if you have a very large CSV i would advice you import it directly into MySQL

function csvToArray($csvFile, $full = false) {
    $handle = fopen ( $csvFile, "r" );
    $array = array ();
    while ( ($data = fgetcsv ( $handle )) !== FALSE ) {
        $array [] = ($full === true) ? $data : $data[0]; // Full array or only ID 
    }

    return $array;
}


$file1 = "file1.csv" ;
$file2 = "file2.csv" ;

$fileData1 = csvToArray($file1);
$fileData2 = csvToArray($file2);


var_dump(array_diff($fileData1,$fileData2));
var_dump(array_intersect($fileData1,$fileData2));
Baba
  • 89,415
  • 27
  • 158
  • 212