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