Compare 2 csv files and highlight row on match

I have following working code to compare 2 csv files - base.csv file to compare to master.csv file using function row_compare. For now I am echoing the master.csv file followed by echoing the match items. I need help to echo only the master.csv file in a table format and highlight the rows that match the base.csv files items.

function row_compare($a, $b)
{
    if ($a === $b) {
        return 0;
    }

    return (implode("",$a) < implode("",$b) ) ? -1 : 1;
}

$file1 = new SplFileObject("master.csv");
$file1->setFlags(SplFileObject::READ_CSV);

$file2 = new SplFileObject("../../base.csv");
$file2->setFlags(SplFileObject::READ_CSV);

foreach ($file1 as $row) {
    $csv_1[] = $row;
}

foreach ($file2 as $row) {
    $csv_2[] = $row;
}

$unique_to_csv1 = array_udiff($csv_1, $csv_2, 'row_compare');
$unique_to_csv2 = array_udiff($csv_2, $csv_1, 'row_compare');

$all_unique_rows = array_merge($unique_to_csv1,$unique_to_csv2);

foreach($all_unique_rows as $unique_row) {
    foreach($unique_row as $element) {
        echo $element . "   ";
    }
    echo '<br />';
}
foreach($masterfilerows as $row){
 $match = in_array($row, $basefilerows) ? 'yes' : 'no';
 echo $masterfilerow.$match;
}

Problem solved by Nigel Ren in Stack Overflow Community

$file2 = new SplFileObject("../../base.csv");
$file2->setFlags(SplFileObject::READ_CSV);

// Read in second file, just create an entry indexed by the name
foreach ($file2 as $row) {
    $csv_2[$row[0]] = 1;
}

$file1 = new SplFileObject("master.csv");
$file1->setFlags(SplFileObject::READ_CSV);

echo "<table>";
// Loop through the master file
foreach($file1 as $row) {
    if ( isset($csv_2[$row[1]]) ) {
        $class = "yellow";
    }
    else    {
        $class = "green";
    }
    echo '<tr class="'.$class.'"><td>';
    echo implode("</td><td>", $row);
    echo '</td></tr>';
}
echo "</table>";

I don’t understand how this actually matches a row.

It matches the first column of the second file against the second column of the first file… but that’s all?

If you look at the Stackoverflow question, it does seem that finding the second column of the second file in the first (only, in fact) column of the first file is what is required here, which isn’t quite what comes across in the start of this thread.

Link: https://stackoverflow.com/questions/61188975/compare-2-csv-files-and-highlight-row-on-match/61190057#61190057

then chorn’s answer is probably better; flatten $file2 into an array, remove the foreach, and then replace the if condition with an in_array($row[1],$file2)… also file2 isnt a CSV, so save yourself the hassle.

$file2 = preg_split("/\r?\n/",file_get_contents("../../base.csv"));

$file1 = new SplFileObject("master.csv");
$file1->setFlags(SplFileObject::READ_CSV);

echo "<table>";
// Loop through the master file
foreach($file1 as $row) {
    echo '<tr class="'.((in_array($row[1],$file2)) ? "green" : "yellow").'"><td>'.implode("</td><td>", $row).'</td></tr>';
}
echo "</table>";

(Spitball, untested)

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.