简体   繁体   English

PHP比较两个CSV文件并突出显示差异

[英]PHP Comparing two CSV files and highlight the difference

I want to compare two CSV files and highlight the difference where it occurs. 我想比较两个CSV文件并突出显示它发生的差异。 At the moment I have a small script that reads both CSV files and prints out the content on to the browser. 目前我有一个小脚本,可以读取两个CSV文件并将内容打印到浏览器上。 these files are structured the same in term of column number but the values in some rows are different and I would like to highlight this difference but not to sure where to start from: 这些文件在列号方面的结构相同,但某些行中的值不同,我想强调这种差异,但不确定从哪里开始:

PHP Code: PHP代码:

<?php
$file = fopen("try.csv","r");
$file2 = fopen("try2.csv", "r");

if(! feof($file)){

    while($data = fgetcsv($file))
    {
        foreach($data as $element){
            echo $element.'   ';
        }

        echo '<br />';


    }
}

echo '<br />';
echo ">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>New File<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<";
echo '<br />';

if(! feof($file2)){

    while($data = fgetcsv($file2))
    {
        foreach($data as $element2){
            echo $element2.'   ';
        }

        echo '<br />';


    }
}


fclose($file);
?>

Knowing that the variables $element & $element2 hold the CSV content how can I find the differences....? 知道变量$element$element2保存CSV内容我怎样才能找到差异....?

fgetcsv returns an array for the current row, so if memory isn't an issue, you could pull in the rows of the first csv and store each row in an array like $csv_1 and then the second as $csv_2 and then use array_udiff to find the non-overlapping rows. fgetcsv为当前行返回一个数组,所以如果内存不是问题,你可以拉入第一个csv的行并将每一行存储在像$csv_1这样的数组中,然后将第二行存储为$csv_2 ,然后使用array_udiff来找到不重叠的行。

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

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

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

$file2 = new SplFileObject("try2.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 />';
}

this code take me time, but now, it can be simplest? 这段代码花了我一些时间,但现在,它可能是最简单的吗?

$filename="nomes.csv"; //lista completa 
$base="track.opened.csv"; //mark if it is on here 
$NOWcodes = array();

$file = fopen($base, 'r'); //registred opened 
while (($line = fgetcsv($file)) !== FALSE) { array_push($NOWcodes, $line[0]);  }
fclose($file);

$file = fopen($filename, 'r'); //all nomes 
while (($line = fgetcsv($file)) !== FALSE) {

if(!in_array($line[0],$NOWcodes)){$inscrito='yellow;';}
else{$inscrito='#9999ff;';} 

echo '<span style="background-color: '.$inscrito.'" title="'.$line[0].'">'.$line[2].'</span><br>'; }
fclose($file);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM