duangouhui0446 2018-11-14 14:27
浏览 64

将行从一个csv文件替换为另一个csv,其中id为samePHP?

I have one excel orginal.csv file

ID     Name     Price
1      Xblue      12
2      Yblue      32
3      Zblue      52

And another copy.csv file

ID     Name     Price
1      Xblue      89
2      Yblue      43
3      Zblue      45

I want to replace rows from orginal.csv to copy.csv where ID is the same. Can I do this manually or maybe somehow using PHP?

I search for some options on the internet, but I only found getcsv and readcsv functions that can't help me in this case. Cause this is something like updating CSV file.

  • 写回答

2条回答 默认 最新

  • dongmei2956 2018-11-14 14:45
    关注

    It may end up in request timeout in PHP because it requires so many loops to do it. If someone can reduce the time complexity of this program then it will work. if it even works it will take a lot of time to do it.

    while(! feof($f_pointer)){ //open old csv to update loop1
    $ar=fgetcsv($f_pointer);  // getting first row
    
    for($i=0;$i<count($ar);$i++){ //loop2 first row array
     $f_pointer2=fopen("new.csv","r"); open new csv to get data
    while(! feof($f_pointer2)){    // loop3 to find ID in new csv
    $ar2=fgetcsv($f_pointer2);    //getting each row in array
    for($j=0;$j<count($ar2);$j++){   //loop4 to compare id of old csv to new csv and update data
    
    if($ar[i] == $ar2[j]){  
    foreach ($ar2 as $fields) {   //loop5
    fputcsv($f_pointer, $fields);
          }
    
        } 
       } 
      } 
     } 
    } 
    ?>
    
    评论

报告相同问题?

悬赏问题

  • ¥15 自适应 AR 模型 参数估计Matlab程序
  • ¥100 角动量包络面如何用MATLAB绘制
  • ¥15 merge函数占用内存过大
  • ¥15 Revit2020下载问题
  • ¥15 使用EMD去噪处理RML2016数据集时候的原理
  • ¥15 神经网络预测均方误差很小 但是图像上看着差别太大
  • ¥15 单片机无法进入HAL_TIM_PWM_PulseFinishedCallback回调函数
  • ¥15 Oracle中如何从clob类型截取特定字符串后面的字符
  • ¥15 想通过pywinauto自动电机应用程序按钮,但是找不到应用程序按钮信息
  • ¥15 如何在炒股软件中,爬到我想看的日k线