dpoh61610 2014-08-21 01:09 采纳率: 100%
浏览 59
已采纳

从CSV读取数据并将唯一行写入外部CSV

I am trying to read read data from a CSV and write it to a new CSV splitting up the data into multiple files where the email address ($row[14]) is unique in each file. The CSV that I'm reading from is already ordered by email address. The loop is working correctly here except that the files that are being created all contain only one row. How can I modify this to write rows into each file that only contain a unique email address.

<?php
 $file = fopen("yahrzeit-4.csv","r");
 $x=1;

 while  ( $row = fgetcsv( $file, ";" ) ) { 

 if ($file) {
 if ($email = $row[14] == $email) {

    $filename = 'mailchimp'.$x.'.csv';  

    $fpR = fopen($filename, 'w');   

    $dataR = array( $row[2], 
                $row[3], 
                $row[14], 
                $row[6] . ' ' . $row[7] . ' ' . $row[8], 
                $row[11] . ' ' . $row[10] . ', ' . $row[12], 
                jdtogregorian ( jewishtojd($Hebmonth, $row[6], 5774 ))
                );

    $email = $row[14];  $x++;   

}
    else { 

    $x=1;

    $fp = fopen('mailchimp.csv', 'w');  

    $data = array ( $row[2], 
                $row[3], 
                $row[14], 
                $row[6] . ' ' . $row[7] . ' ' . $row[8], 
                $row[11] . ' ' . $row[10] . ', ' . $row[12], 
                jdtogregorian ( jewishtojd($Hebmonth, $row[6], 5774 ))
              );

    }

    $email = $row[14];

}
fputcsv($fpR, $dataR);      
fputcsv($fp, $data);

}       

fclose($fp);
fclose($fpR);

?>
  • 写回答

1条回答 默认 最新

  • doushi1964 2014-08-21 02:34
    关注

    If I understand correctly it looks like you're trying to create a CSV file containing the records from another CSV file. The exception is if you find duplicate email addresses you want these to go into a different file for each duplicate maintaining as few files as you can as long as the addresses are unique in each?

    I obviously haven't checked this as I don't have your data but this should hopefully do what you're after, splitting non-unique email addresses into the next available file (for that address).

    <?php
        $file = fopen("yahrzeit-4.csv","r");
        $addresses = array(); //This will hold counters for each address
        $fp = fopen('mailchimp.csv', 'w'); //This is the first list
        $fp2 = array(); //This will hold handles for each subsequent csv, 1 for each non-unique address (although it may hold more than one address unique to the file)
    
        if($file) {
            while  ( $row = fgetcsv( $file, ";" ) ) { //Read file
                if(isset($addresses[$row[14]])) { //Email has been found before at least once
                    $targetfile = $addresses[$row[14]]; //Check the counter to find the next csv to write to for this particular address
                    if(!isset($fp2[$targetfile])) { //Check if it has already been opened
                        $fp2[$targetfile] = fopen('mailchimp'.$targetfile.'.csv', 'w'); //If not open it and store it in the array of file handles for later use
                    }
    
                    $dataR = array( 
                        $row[2], 
                        $row[3], 
                        $row[14], 
                        $row[6] . ' ' . $row[7] . ' ' . $row[8], 
                        $row[11] . ' ' . $row[10] . ', ' . $row[12], 
                        jdtogregorian ( jewishtojd($Hebmonth, $row[6], 5774 ))
                    );
    
                    fputcsv($fp2[$targetfile], $dataR); //Write data to this file handle
                    $addresses[$row[14]]++; //Increment counter for this email address so the next write will go into the next sequential file.
    
                } else { //This is the standard write if the address is not a duplicate
                    $data = array (
                        $row[2], 
                        $row[3], 
                        $row[14], 
                        $row[6] . ' ' . $row[7] . ' ' . $row[8], 
                        $row[11] . ' ' . $row[10] . ', ' . $row[12], 
                        jdtogregorian ( jewishtojd($Hebmonth, $row[6], 5774 ))
                    );
    
                    fputcsv($fp, $data);
                    $addresses[$row[14]] = 1;
                }
            }
    
            foreach($fp2 as $handle) { //Close all handles
                fclose($handle);
            }
            fclose($fp);
        }
    
    ?>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 公交车和无人机协同运输
  • ¥15 stm32代码移植没反应
  • ¥15 matlab基于pde算法图像修复,为什么只能对示例图像有效
  • ¥100 连续两帧图像高速减法
  • ¥15 组策略中的计算机配置策略无法下发
  • ¥15 如何绘制动力学系统的相图
  • ¥15 对接wps接口实现获取元数据
  • ¥20 给自己本科IT专业毕业的妹m找个实习工作
  • ¥15 用友U8:向一个无法连接的网络尝试了一个套接字操作,如何解决?
  • ¥30 我的代码按理说完成了模型的搭建、训练、验证测试等工作(标签-网络|关键词-变化检测)