简体   繁体   English

PHP-显示两个CSV文件之间的差异

[英]PHP - Display differences between two CSV files

I have written a small script which upload two csv files and compare them. 我写了一个小脚本,可以上传两个csv文件并进行比较。

//set files upload directory //设置文件上传目录

 $target_dir1 = "uploads/old/";
 $target_file1 = $target_dir1 . basename($_FILES["fileToUpload1"]["name"]);

 $target_dir2 = "uploads/new/";
 $target_file2 = $target_dir2 . basename($_FILES["fileToUpload2"]["name"]); 

 $uploadOk = 1;

//Upload files //上传文件

 if ($uploadOk == 0) {
 echo "<BR> Sorry, your files were not uploaded. <BR>";
 } else {
 if (move_uploaded_file($_FILES["fileToUpload1"]["tmp_name"],        $target_file1)) {
    echo "<BR> The 1st file ". basename( $_FILES["fileToUpload1"]["name"]). " has been uploaded. <BR>";
} else {
   echo "<BR> Sorry, there was an error uploading your 1st file. <BR>";
}

if (move_uploaded_file($_FILES["fileToUpload2"]["tmp_name"], $target_file2)) {
    echo "<BR> The 2nd file ". basename( $_FILES["fileToUpload2"]["name"]). " has been uploaded.<BR>";
} else {
   echo "<BR> Sorry, there was an error uploading your 2nd file. <BR>";
}  
} 

//Get contetnt 1st file //获取竞争的第一个文件

$table1 = Array();
 $filehandle1 = fopen($target_file1, "r") ;
 if($filehandle1 !== FALSE) {
while(! feof($filehandle1)) {          // feof end of file
$data1 = fgetcsv($filehandle1, 1000, ",");
array_push($table1, $data1); 
  }
  }
 fclose($filehandle1);  

//Get content 2nd file //获取内容第二个文件

 $table2 = Array();
  $filehandle2 = fopen($target_file2, "r") ;
 if($filehandle2 !== FALSE) {
  while(! feof($filehandle2)) {
  $data2 = fgetcsv($filehandle2, 1000, ",");
 array_push($table2, $data2); 
 }
 }
 fclose($filehandle2);  

//Find difference between these two files //找到这两个文件之间的差异

$headers= array();
$headers =  $table1[0];  

  $i= 0;
  foreach ($table1 as $table) {  
 echo '<BR>';     
 $diff = array_diff($table2[$i], $table);
  if(!empty($diff)) { 
  print_r($diff);
 $chiave= key($diff);
 echo  $headers[$chiave];
 };
 echo '<BR>';
 $i++;
 } 

And this is the error I get, however difference between the two files are dispalyed correctly: 这是我得到的错误,但是正确显示了两个文件之间的差异:

   Warning: array_diff(): Argument #1 is not an array in /var/www/csv_files/upload.php on line 67 Call Stack: 0.0053 337384 1. {main}() /var/www/csv_files/upload.php:0 0.0064 367220 2. array_diff() /var/www/csv_files/upload.php:67 

You get this error because the first argument is not a array where one is expected. 因为第一个参数不是预期的数组,所以会出现此错误。 You are now checking a table with the nth element of a array but not the whole array. 现在,您正在使用数组的第n个元素而不是整个数组检查表。 I think you are making a mistake in thinking table2 is a 2 dimensional array, and it's not. 我认为您在认为table2是二维数组时犯了一个错误,事实并非如此。 It is used a one dimensional array with nth data2 elements. 它用于具有第n个data2元素的一维数组。

Hope this helps! 希望这可以帮助!

Seems yes, sometimes table2 is empty or those CSV files have different amount of rows - as result that warning. 似乎可以,有时table2为空,或者那些CSV文件具有不同的行数-结果是警告。

So - you need add extra checks if $table2[$i] is not null. 因此-如果$ table2 [$ i]不为null,则需要添加额外的检查。

Just a bit another variant from me - how to read file faster (Get content 1st and second file): 我的另一个变种-如何更快地读取文件(获取第一和第二个文件的内容):

$table1 = file($target_file1);
$table2 = file($target_file2);

And then you can do same things as before, with extra tests: 然后,您可以通过额外的测试来做与以前相同的事情:

if (count($table1)) {
  $headers = str_getcsv($table1[0]);

  foreach ($table1 as $key => $table)   {
    if (!isset($table2[$key])) {
        echo 'Row ' . ($key+1) . ' is not exists in second CSV file.';
    } else {
        $diff = array_diff(str_getcsv($table2[$key]), str_getcsv($table));
        // Here is code as in your example
        print_r($diff);
        $chiave = key($diff);
        echo  $headers[$chiave];
    }
  }
}

Good luck! 祝好运! :) :)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM