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);
        }
    
    ?>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 对于相关问题的求解与代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料