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

I have written a small script which upload two csv files and compare them.

//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. I think you are making a mistake in thinking table2 is a 2 dimensional array, and it's not. It is used a one dimensional array with nth data2 elements.

Hope this helps!

Seems yes, sometimes table2 is empty or those CSV files have different amount of rows - as result that warning.

So - you need add extra checks if $table2[$i] is not 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! :)