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