如何删除csv文件中的重复电子邮件

时间:2015-11-17 05:19:18

标签: php email csv

如何删除不同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);
?>

for remove email only > this php code is work.

the problem is if I want to remove emails under "Multiple columns" is Not work 任何人都可以提供帮助

如果你能帮助我,我将不胜感激,谢谢

1 个答案:

答案 0 :(得分:0)

我建议将其封装在一个流式传输每一行的函数中,构建一个字段数组 - &gt;然后检查值以查看该值是否在您要删除的数据中,如果不是,则将该行写入out文件。

像...一样的东西。

<?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's not, then it's a valid line
                    fputcsv($writeHandle, $line);  
                }
            }
        }
        fclose($readHandle);
        fclose($writeHandle);
    }
    // Return 
    return true;
}