duan0708676887 2017-01-12 15:11
浏览 45

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 
  • 写回答

2条回答 默认 最新

  • douzhang6646 2017-01-12 15:18
    关注

    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!

    评论

报告相同问题?

悬赏问题

  • ¥20 软件测试决策法疑问求解答
  • ¥15 win11 23H2删除推荐的项目,支持注册表等
  • ¥15 matlab 用yalmip搭建模型,cplex求解,线性化处理的方法
  • ¥15 qt6.6.3 基于百度云的语音识别 不会改
  • ¥15 关于#目标检测#的问题:大概就是类似后台自动检测某下架商品的库存,在他监测到该商品上架并且可以购买的瞬间点击立即购买下单
  • ¥15 神经网络怎么把隐含层变量融合到损失函数中?
  • ¥15 lingo18勾选global solver求解使用的算法
  • ¥15 全部备份安卓app数据包括密码,可以复制到另一手机上运行
  • ¥20 测距传感器数据手册i2c
  • ¥15 RPA正常跑,cmd输入cookies跑不出来