2

I need to get the difference between two CSV files, whereby the latest file in this case file2.csv overwrites rows with same values with an updated version and removes rows with identical data.

file1.csv

GHI, 0, 0
ABC, 12, 1
DEF, 10, 1

file2.csv

ABC, 8, 1
DEF, 10, 1
GHI, 2, 0

The final CSV should be like this :

ABC, 8, 1
GHI, 2, 0

the php code:

<?php
    $file1 = file('file1.csv');
    $file2 = file('file2.csv');

    sort($file1);
    sort($file2);

    var_dump($file1);
    var_dump($file2);

    $diff =  array_diff($file2, $file1);

    var_dump($diff);

    ?>

returns this

ABC, 8, 1
DEF, 10, 1
GHI, 2, 0

Now when I change manually file1.csv to this:

ABC, 12, 1
DEF, 10, 1
GHI, 0, 0

it returns this as expected:

ABC, 8, 1
GHI, 2, 0

When I dump the sorted array it seems the sort function works as it returns just like when I change the order manually

ABC, 12, 1
DEF, 10, 1
GHI, 0, 0

Yet it seems not to produce the same end result. Any clues?

1 Answer 1

1

file splits the file into lines, but each line still has its ending newline character. The last line, however, does not. Therefore, it is not equal to the same line contents in the middle of a file.

To fix this, you can either add a newline to the last element:

$file1[count($file1)-1] .= PHP_EOL;
$file2[count($file2)-1] .= PHP_EOL;

Or you could strip them from all the lines:

array_walk($file1,function(&$a) {$a = trim($a);});
array_walk($file2,function(&$a) {$a = trim($a);});
Sign up to request clarification or add additional context in comments.

2 Comments

Thank you I just looped over the values and noticed the missing newline, of course the dump didn't showed that. Thanks
@Kolink add FILE_IGNORE_NEW_LINES into your answer, as deceze mentioned.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.