Is there a simple way to find and remove duplicate rows from a CSV file?
Sample test.csv file:
row1 test tyy......
row2 tesg ghh
row2 tesg ghh
row2 tesg ghh
....
row3 tesg ghh
row3 tesg ghh
...
row4 tesg ghh
Expected results:
row1 test tyy......
row2 tesg ghh
....
row3 tesg ghh
...
row4 tesg ghh
Where can I start to accomplish this with PHP?
A straight-to-the point method would be to read the file in line-by-line and keep track of each row you’ve previously seen. If the current row has already been seen, skip it.
Something like the following (untested) code may work:
This code uses
fgetcsv()and uses aspacecomma as your column-delimiter (based on the sample-data in yourquestioncomment).Storing every line that has been seen, as above, will assure to remove all duplicate lines in the file regardless of whether-or-not they’re directly following one another or not. If they’re always going to be back-to-back, a more simple method (and more memory conscious) would be to store only the last-seen line and then compare against the current one.
UPDATE (duplicate lines via the SKU-column, not full-line)
Based on sample data provided in a comment, the “duplicate lines” aren’t actually equal (though they are similar, they differ by a good number of columns). The similarity between them can be linked to a single column, the
sku.The following is an expanded version of the above code. This block will parse the first line (column-list) of the CSV file to determine which column contains the
skucode. From there, it will keep a unique list of SKU codes seen and if the current line has a “new” code, it will write that line to the new “unique” file usingfputcsv():Overall, this method is far-more memory friendly as it doesn’t need to save a copy of every line in memory (only the SKU codes).