Remove duplicate strings

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
jcook.linux
Forum Newbie
Posts: 1
Joined: Mon Jan 18, 2010 1:42 pm

Remove duplicate strings

Post by jcook.linux »

I have a project I have been working on and for the life of me I can not figure it out. I have a list of string, I need to remove the duplicates. I have try adding them to a array and do array_unique. but that did not work. I thought of comparing $a == $b with a off set of one so $b would be one behind, but I cant not think of how to do that.

List:

10.5.0.109www.foxnews.com
10.5.0.109www.foxnews.com
10.5.0.109www.foxnews.com
10.5.0.109www.foxnews.com
10.5.0.109www.foxnews.com
10.5.0.109ad.doubleclick.net
10.5.0.109ad.doubleclick.net
10.5.0.109ad.doubleclick.net
10.5.0.109adfarm.mediaplex.com
10.5.0.109ad.doubleclick.net
10.5.0.109analytics.live.com
10.5.0.109ad.doubleclick.net
10.5.0.109ad.doubleclick.net


Any Help would be great.

Jeff
User avatar
AbraCadaver
DevNet Master
Posts: 2572
Joined: Mon Feb 24, 2003 10:12 am
Location: The Republic of Texas
Contact:

Re: Remove duplicate strings

Post by AbraCadaver »

Maybe you didn't do it correctly, cuz array_unique() works great:

Code: Select all

$a = array(
'10.5.0.109www.foxnews.com',
'10.5.0.109www.foxnews.com',
'10.5.0.109www.foxnews.com',
'10.5.0.109www.foxnews.com',
'10.5.0.109www.foxnews.com',
'10.5.0.109ad.doubleclick.net',
'10.5.0.109ad.doubleclick.net',
'10.5.0.109ad.doubleclick.net',
'10.5.0.109adfarm.mediaplex.com',
'10.5.0.109ad.doubleclick.net',
'10.5.0.109analytics.live.com',
'10.5.0.109ad.doubleclick.net',
'10.5.0.109ad.doubleclick.net',
);
 
print_r(array_unique($a));
Yields:

Code: Select all

Array
(
    [0] => 10.5.0.109www.foxnews.com
    [5] => 10.5.0.109ad.doubleclick.net
    [8] => 10.5.0.109adfarm.mediaplex.com
    [10] => 10.5.0.109analytics.live.com
)
mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.
Post Reply