php sort for dups in two files

OK here's the problem I have two files block.txt and edit.txt
they contain domain entries on per line (
I would like to merge the twp file together but not have any dups. Both files have different amounts a entries (2400 and 420). Here's what I have so far:



$array = @File("$type");
$total = count($array);


// Time to open the entry file

$fp=fopen("$filename","r")or die("Can not open file");

// Read the file into an array

if ($fp) {  $array = explode("\n", fread($fp, filesize($filename))); }

// Close the file


// Echo the second entry in the file

echo "sencond entery in new file: $array[1]<br>\n";

// Count the number of entries in the file


// Echo the number of entries.

echo "entery count total: $entry<br>\n";

// Open the blocking file

$fp=fopen("block","r")or die("can not open the block file");
if($fp) { $arrayb = explode("\n", fread($fp, filesize(block))); }

echo "first entery in block file: $arrayb[0]<br>\n";
echo "total blocking: $blocking\n<br>";

for($k = 0 ; $k < $blocking ; $k++) {
      //if(trim($array[$k] != $arrayb[$d])) {
      for($d = 0; $d < $entry ; $d++) {      
                  if(trim($array[$k] != $arrayb[$d])) {
                        //$outputfile .= "$array[$k]\n";
                        echo "file ok $k add:$array[$k] and block:$arrayb[$d]<br>";
            if($t == 1){
                  $outputfile .= "$array[$k]\n";
            else {$t=0;}
      //else { $d++; }

echo $outputfile;

I know it's no right and right now I only need it to print a web page I can change it to files later.

Who is Participating?
shmertConnect With a Mentor Commented:
Just an implementation of snoyes' post:

$array = array_unique(array_merge(file('block.txt'), file('edit.txt')));

// Note: the line breaks will still be there for each element, so you may want to iterate through and trim() each entry.
// This should be a very speedy way to do it, though.

foreach($array AS $key=>$value) {
    $array[$key] = rtrim($value);
You could read everything from both files into an array, then use array_unique() to delete all the duplicates, and write the results back out to a file.
If you're running in a unix environment, you can use the "sort" command to do it for you.

$array = explode("\n", `cat block.txt > tmp.txt; cat edit.txt >> tmp.txt; sort -u tmp.txt`);


even easier:

$array = explode("\n", `sort -u block.txt edit.txt`);
jscartAuthor Commented:
THanks for the suggestions I'll start trying them and then score.
All Courses

From novice to tech pro — start learning today.