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

?>

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

?>