PHP Duplicates

Soldato
Joined
24 Nov 2002
Posts
16,378
Location
38.744281°N 104.846806°W
I have a set of data like below that contains duplicates.

I'd like to remove all the duplicates - leaving just one "version" of the line...

example data: (not duplicate)

xx_453845.1 1500
xx_453845.1 1500
xy_455456.1 1600
etc...

Surely there is an easy way to do this?

Would idealy like input to be text area and output to be echo, for now.

(aside and unrelated, how do you get php to output tabs.. at the moment it outputs as above: blah_number, how do I get blah_tab_number)
 
What is the source of the data set? If you have an array, you can strip duplicates with array_unique().

As for tabs, you use the control character "\t" e.g.
Code:
$tabs = "$blah\t$number";
Make sure that \t is within double quotes, so it is not treated as a literal string.
 
Augmented said:
Make sure that \t is within double quotes, so it is not treated as a literal string.
Bingo.

as for the the source, it is a text file at the moment.... I wanted to just cut and paste into text area....

something like array_unique($data); where the $data is contents of file/textarea....
 
<?php
$list = file('data.dat');
$list = array_unique($list);
file_put_contents('data.dat', implode('', $list));
?>

Doesn't work....

"Fatal error: Call to undefined function: file_put_contents()"

Shall I guess I have to use fopen, fclose... now?
 
Last edited:
jdickerson said:
<?php
$list = file('data.dat');
$list = array_unique($list);
file_put_contents('data.dat', implode('', $list));
?>

Doesn't work....

"Fatal error: Call to undefined function: file_put_contents()"

Shall I guess I have to your fopen, fclose... now?

Yep, file_put_contents is PHP5 only.
 
robmiller said:
Yep, file_put_contents is PHP5 only.

RAGE.

I thought I had php5 - oh no wait.. I think I do on the pc at home, not the host..

Bah. Just did it on my box instead.

Not sure if it has worked 'properly' though....
 
Last edited:
Hmmmm...

Example data is:

Code:
NP_057850.1 155030 
NP_057850.1 155030 
NP_579876.2 155030 
NP_057852.2 155807 
NP_057853.1 155871 
NP_579876.2 155030 
NP_057854.1 155908 
NP_057854.1 155908 
NP_057854.1 155908 
NP_057854.1 155908 
NP_057854.1 155908 
NP_057855.1 155945 
NP_057855.1 155945 
NP_057855.1 155945 
NP_579881.1 155030 
NP_057852.2 155807 
NP_579876.2 155030 
NP_579876.2 155030 
NP_579876.2 155030 
NP_705928.1 155348 
NP_705928.1 155348 
NP_579880.1 155030 
NP_579876.2 155030 
NP_579883.1 155030 
NP_579883.1 155030 
NP_057853.1 155871 
NP_579883.1 155030 
NP_579883.1 155030 
NP_057853.1 155871 
NP_579883.1 155030 
NP_579883.1 155030 
NP_579883.1 155030 
NP_579876.2 155030 
NP_579876.2 155030 
NP_057853.1 155871 
NP_579876.2 155030 
NP_579876.2 155030 
NP_057853.1 155871 
NP_057850.1 155030 
NP_057850.1 155030 
NP_057855.1 155945 
NP_579876.2 155030 
NP_579876.2 155030 
NP_057853.1 155871 
NP_579881.1 155030 
NP_057852.2 155807 
NP_057853.1 155871 
NP_057853.1 155871 
NP_057853.1 155871 
NP_057853.1 155871 
NP_057853.1 155871 
NP_057853.1 155871 
NP_057853.1 155871 
NP_579876.2 155030 
NP_579876.2 155030 
NP_057853.1 155871 
NP_579883.1 155030 
NP_579883.1 155030 
NP_579876.2 155030 
NP_579876.2 155030 
NP_057853.1 155871 
NP_579876.2 155030 
NP_057853.1 155871 
NP_057853.1 155871 
NP_057853.1 155871 
NP_057854.1 155908 
NP_579881.1 155030 
NP_057852.2 155807 
NP_057853.1 155871 
NP_057853.1 155871 
NP_057853.1 155871 
NP_057853.1 155871 
NP_057853.1 155871 
NP_057853.1 155871 
NP_057853.1 155871 
NP_057853.1 155871 
NP_057853.1 155871 
NP_057853.1 155871 
NP_057853.1 155871 
NP_579880.1 155030 
NP_579876.2 155030 
NP_057850.1 155030 
NP_705928.1 155348 
NP_579876.2 155030 
NP_057850.1 155030 
NP_057853.1 155871 
NP_057853.1 155871 
NP_057853.1 155871 
NP_057850.1 155030 
NP_057853.1 155871 
NP_579876.2 155030 
NP_579880.1 155030 
NP_057850.1 155030 
NP_057851.1 155459 
NP_579883.1 155030

How could I remove duplicate lines from this?

ABove method seems to remove more than duplicates...
 
robmiller said:
Code:
fwrite($fh=fopen('data2.txt','w'),join('',array_unique(file('data.txt'))));fclose($fh);

Works for me :k

I'm just paranoid it is taking away more than it should ;)

Cheers rob!
 
Last edited:
Back
Top Bottom