Как удалить дублированный адрес электронной почты в другом файле csv
Пример: у меня есть 10.000 адресов электронной почты (all_members.csv) после отправки> Затем я получил 2550 недействительных сообщений электронной почты (invalid.csv)
Я хочу удалить этот "недействительный адрес электронной почты"
Мой код "
<?php
$all = file('all_email.csv'); // all_members.csv
$invalid = file('invalid.csv'); // invalid_email.csv
$correctEmails=array_diff($all, $invalid);
foreach ($correctEmails as $email) { echo $email."<br>"; }
$result = array_intersect($all,$invalid);
?>
для удаления только электронной почты> этот PHP-код работает.
проблема в том, что если я хочу удалить электронные письма в разделе "Несколько столбцов", это не работает, кто может помочь
Я был бы очень признателен, если бы вы смогли мне помочь, спасибо
Я бы рекомендовал инкапсулировать это в функцию, которая передает каждую строку, строит массив значений полей ->, а затем проверяет, имеет ли значение в данных, которые вы хотите удалить, если это не так, напишите строку в выходной файл.
Что-то вроде...
<?php
/**
* Given a CSV file to read, the delimiter, and what to remove, return the filtered CSV data
* @param $filename string The /path/to/file.csv
* @param $outputFile string Where to write the output CSV data to
* @param $delimiter string How the fields are delimited in the CSV
* @param $removeHeader string The header to remove data from
* @param $removeData array The data to omit from the output
*
* @return boolean
**/
function remove_duplicates($filename, $outputFile, $delimiter=',', $removeHeader, $removeData)
{
// If the file doesn't exist or isn't readable - return false
if(!file_exists($filename) || !is_readable($filename)) {
return false;
}
$header = null;
$validData = [];
$writeHandle = fopen($outputFile, 'w');
if (false !== ($readHandle = fopen($filename, 'r'))) {
//While there are rows in the CSV, get this as an array of values
while (false !== ($row = fgetcsv($readHandle, 1000, $delimiter))) {
//On the first iteration, get the headers from the CSV
if (!$header) {
$header = $row;
fputcsv($writeHandle, $header);
} else {
// Combine the headers with the row to create an associative array representing a line
$line = array_combine($header, $row);
// Looking at the removeHeader field in this line, check to see if the value is in removeData
if (!in_array($line[$removeHeader], $removeData) {
// If it not, then it a valid line
fputcsv($writeHandle, $line);
}
}
}
fclose($readHandle);
fclose($writeHandle);
}
// Return
return true;
}