Solved

php sort for dups in two files

Posted on 2003-11-02
5
241 Views
Last Modified: 2007-12-19
OK here's the problem I have two files block.txt and edit.txt
they contain domain entries on per line (.aol.com)
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:

set_time_limit(50);

$MAX=99999;
$free=0;
$empty="empty\n";

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

$free=$total;

// Time to open the entry file

$filename="Edit2.txt";
$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

fclose($fp);

// 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

$entry=count($array);


// 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))); }
fclose($fp);

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


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

TIA
0
Comment
Question by:jscart
5 Comments
 
LVL 33

Expert Comment

by:snoyes_jw
Comment Utility
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.
0
 
LVL 11

Accepted Solution

by:
shmert earned 125 total points
Comment Utility
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);
}
0
 
LVL 3

Expert Comment

by:wide_awake
Comment Utility
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`);

-mark

0
 
LVL 3

Expert Comment

by:wide_awake
Comment Utility
even easier:

$array = explode("\n", `sort -u block.txt edit.txt`);
0
 
LVL 1

Author Comment

by:jscart
Comment Utility
THanks for the suggestions I'll start trying them and then score.
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Both Easy and Powerful How easy is PHP? http://lmgtfy.com?q=how+easy+is+php (http://lmgtfy.com?q=how+easy+is+php)  Very easy.  It has been described as "a programming language even my grandmother can use." How powerful is PHP?  http://en.wikiped…
Deprecated and Headed for the Dustbin By now, you have probably heard that some PHP features, while convenient, can also cause PHP security problems.  This article discusses one of those, called register_globals.  It is a thing you do not want.  …
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

763 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now