我目前正在编写一个简单的PHP脚本/站点来保存第3个文件中2个csv文件的差异。
目前我唯一的问题是,我想忽略不时变化的特定行(客户ID)。
我正在编写一个新闻通讯系统,我有2个带有客户数据的csv文件,我正在检查,如果自上一个csv文件后电子邮件地址发生了变化。
如何忽略客户ID,我不能只删除该列,因为我需要该列以供日后使用。
这是我的结构:
CSV文件1(旧数据库):
customerid,sex,name,firstname,zip,email
1,male,smith,will,1234,will.smith@gmail.com
2,male,doe,john,7367,john@doe.com
3,female,doe,anna,7367,anne@doe.com
CSV文件2(新数据库):
customerid,sex,name,firstname,zip,email
1,male,smith,will,2224,will.smith@gmail.com
7,male,doe,john,7367,john@gmail.com
20,female,doe,anna,7367,anne@doe.com
正如您所看到的,在较新的文件中,John和Anna Doe的客户ID发生了变化,应该忽略这一点。
不应忽视的是,John Doe的电子邮件和Will Smith的邮箱发生了变化。第二步“完美无缺”。
以下是我的完整代码:http://pastebin.com/bt7Pj3MP(约30行)。有重要的部分:
$file1 = file('2015-07-01.csv', FILE_IGNORE_NEW_LINES);
$file2 = file('2015-07-09.csv', FILE_IGNORE_NEW_LINES);
sort($file1);
sort($file2);
$diff = array_diff($file2, $file1);
array_unshift($diff, $_POST['csv_cols']);
$output = substr(md5(rand()), 0, 5). "-output.csv";
file_put_contents($output, implode(PHP_EOL, $diff));
unlink($file1_name);
unlink($file2_name);
header('Content-Description: Download ' . $output);
header('Content-Type: application/force-download');
header("Content-Type: application/download");
header("Content-Length: " . filesize($output));
header("Content-disposition: attachment; filename=\"" . basename($output) . "\"");
readfile($output);
unlink($output);
exit;
谢谢!
弗洛里安