【问题标题】:Reading data from a CSV and writing unique rows to external CSVs从 CSV 读取数据并将唯一行写入外部 CSV
【发布时间】:2014-08-21 01:09:49
【问题描述】:

我正在尝试从 CSV 读取读取数据并将其写入新的 CSV,将数据拆分为多个文件,其中电子邮件地址 ($row[14]) 在每个文件中都是唯一的。我正在阅读的 CSV 已按电子邮件地址排序。循环在这里正常工作,只是正在创建的文件都只包含一行。如何修改它以将行写入每个仅包含唯一电子邮件地址的文件。

<?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);

?>

【问题讨论】:

    标签: php csv


    【解决方案1】:

    如果我理解正确,您似乎正在尝试创建一个 CSV 文件,其中包含来自另一个 CSV 文件的记录。例外情况是,如果您发现重复的电子邮件地址,您希望将它们放入不同的文件中,以便为每个重复项保留尽可能少的文件,只要每个地址中的地址都是唯一的?

    我显然没有检查过这个,因为我没有你的数据,但这应该可以满足你的要求,将非唯一的电子邮件地址拆分到下一个可用的文件中(针对该地址)。

    <?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);
        }
    
    ?>
    

    【讨论】:

    • 我收到此错误:fputcsv() 期望参数 2 为数组,此行为 null:fputcsv($fp, $dataR);
    • 对不起,我不确定我的回复发生了什么,但上面有一个错字。我已经编辑了我的答案,希望它现在应该是。
    • 错误消失了,但它仍然只在每个 CSV 中放入一行。
    • 我已经创建了一个基本测试,现在它看起来可以工作了。我在 isset 检查中错过了“2”。我还删除了一些其他不必要的或可能造成麻烦的东西。
    • 您能解释一下这是如何工作的吗?我正在尝试在此处添加另一项检查重复项,但很难理解这实际上是如何工作的。我需要在唯一地址之后添加另一项检查,以检查唯一名称并排除重复项。
    猜你喜欢
    • 2018-03-19
    • 2023-04-04
    • 2019-10-05
    • 1970-01-01
    • 1970-01-01
    • 2021-04-28
    • 2020-06-22
    • 1970-01-01
    • 2021-01-01
    相关资源
    最近更新 更多