How to check duplicate rows in a csv file using php?

Member

by adan , in category: PHP General , 7 months ago

How to check duplicate rows in a csv file using php?

Facebook Twitter LinkedIn Telegram Whatsapp

1 answer

Member

by dana , 3 months ago

@adan 

To check for duplicate rows in a CSV file using PHP, you can read the CSV file line by line and keep track of the rows you have already processed. Here is a simple example that demonstrates how this can be done:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
$file = 'data.csv';
$handle = fopen($file, 'r');
$rows = array();

while (($data = fgetcsv($handle, 1000, ',')) !== FALSE) {
    $row = implode(',', $data);
    
    // Check if the row already exists in the array
    if (in_array($row, $rows)) {
        echo "Duplicate row found: " . $row . PHP_EOL;
    } else {
        $rows[] = $row;
    }
}

fclose($handle);


In this code snippet, we open the CSV file using fopen() and read it line by line using fgetcsv(). We then convert each row into a string and check if it already exists in the $rows array. If it does, we output a message indicating a duplicate row has been found. Otherwise, we add the row to the $rows array.


This is a basic example and can be expanded based on your specific requirements, such as ignoring certain columns or considering only specific fields for checking duplicates.