Roberto C.
Roberto C.

Reputation: 73

Compare 2 csv file with 1 column each in php

I have to compare 2 csv files where each file got 1 column:

file1.csv:

column1

9788800000000
9788800000001
9788800000002
9788800000003
...

file2.csv:

column1

9788800000000
9788800000001
9788800000002
9788800000006
9788800000007
9788800000008
...

I have to do something when some EAN code isn't found in the second csv file (file2.csv). For example the code 9788800000003 it's ONLY present in file1.csv.

Now, I have to update a table in DB when an EAN code in the first file, isn't found in the second file and set it to '0' (in this case, the ean 9788800000003 have to be change in 0 quantity):

mysql_query("UPDATE $update_table SET quantity='0' WHERE ean13='$ean13'")

how to compare the column for differences ?

Upvotes: 0

Views: 674

Answers (1)

AbraCadaver
AbraCadaver

Reputation: 78994

Something like this:

$file1 = file('file1.csv');
$file2 = file('file2.csv');

$diff  = array_diff($file1, $file2);
$list  = implode(',', $diff);

$query = "UPDATE $update_table SET quantity='0' WHERE ean13 IN ($list)";
  1. Read files into an array of lines
  2. Get an array of the differences
  3. Generate a list of the EAN differences
  4. Use IN in the WHERE clause

Upvotes: 3

Related Questions