Thursday, January 14, 2010

Compare data in two CSV files

[root@nabeelmoidu html]# cat compare.php
 

// Input of the format
// field1 in file1 (eg, 2446)
// field2,field1 in file2 (Nabeel, 2446)

$filename='file1.csv';
$handle = fopen("$filename", "r");
$ctr=0;

while (($read = fgetcsv($handle, 1000, ",")) !== FALSE){
$num[$ctr]= (int)$read[0];
// echo $num[$ctr]."
";
$ctr = $ctr + 1;
}

$filename1='file2.csv';
$handle1 = fopen("$filename1", "r");
$ctr1=0;
while (($rawname = fgetcsv($handle1, 1000, ",")) !== FALSE)
{ $ext[$ctr1]= (int)$rawname[0];
$name[$ctr1]= $rawname[1];
// echo $ext[$ctr1]."
";
$ctr1 = $ctr1 + 1;
}

for ($n=0; $n <= $ctr1; $n++) { $match[$n]=1; }

for ($x=0; $x <= $ctr; $x++)
{
for ($y=0; $y <= $ctr1; $y++) {
if ($num[$x]==$ext[$y]){$match[$y]=0;}
else {};
}
}

for ($y=0; $y <= $ctr1; $y++) {
if ($match[$y])
{ echo $name[$y]." | ".$ext[$y]."
";}
else {};
}

fclose($handle);
fclose($handle1);
?>

2 comments:

Anonymous said...

this is doing comparison on row or coloumn?

n said...

Column by column in each row.