【问题标题】:How to remove duplicate emails in csv file如何删除csv文件中的重复邮件
【发布时间】:2015-11-17 17:30:28
【问题描述】:

如何删除不同 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 任何人都可以提供帮助

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

【问题讨论】:

标签: php email csv


【解决方案1】:

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

类似...

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

【讨论】:

    猜你喜欢
    • 2015-05-24
    • 2022-08-02
    • 2019-01-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多