1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

removing duplicate entries in a list

Discussion in 'PHP' started by relixx, Aug 10, 2006.

  1. #1
    I have a list of roughly 2300 keywords I need to sort through (ouch), and as they're simply copied from adwords there are a lot of duplicates. What Im needing is a quick script that'll take the list and remove any duplicate entries, only leaving the original one, eg

    dog
    dog
    cat
    mouse
    fish
    bird
    Bush
    chimp
    mouse
    rat
    dog

    becomes

    dog
    cat
    mouse
    fish
    bird
    rat

    I was thinking of using two arrays, where all the keywords are stored in the first and looped through, adding it to the second if it doesn't already exist there, but that can be a bit of a resource-heavy so I was wondering if anyone knows of a quicker way, maybe something similar to array_intersect()?
     
    relixx, Aug 10, 2006 IP
  2. klown

    klown Peon

    Messages:
    2,093
    Likes Received:
    115
    Best Answers:
    0
    Trophy Points:
    0
    #2
    i would put them all into one array then do an array sort. then delete the ones that are done twice.. heres an example.

    	
            $query = "SELECT * FROM store ORDER BY 'country'";
    	//note you would do a asort($array); instead
    	$result = mysql_query($query) or die(mysql_error());
    	$num_rows=mysql_num_rows($result);
    	$x=0;	
    	while($row = mysql_fetch_array($result))
    	{
              if ($arrcountry[$x-1]!=$row['country']) {$arrcountry[$x]=$row['country']; ++$x;}
    	}
    
    PHP:
     
    klown, Aug 10, 2006 IP
  3. relixx

    relixx Active Member

    Messages:
    946
    Likes Received:
    54
    Best Answers:
    0
    Trophy Points:
    70
    #3
    thanks for the help, buddy of mine modified ur script to my needs for me :)
     
    relixx, Aug 10, 2006 IP
  4. klown

    klown Peon

    Messages:
    2,093
    Likes Received:
    115
    Best Answers:
    0
    Trophy Points:
    0
    #4
    no prob, i would have posted a better version of the script more applicable to your needs but didnt have time to look up the page i had the source on.
     
    klown, Aug 10, 2006 IP
  5. KevinJB

    KevinJB Peon

    Messages:
    170
    Likes Received:
    5
    Best Answers:
    0
    Trophy Points:
    0
    #5
    array_unique() is a php function that removes duplicate entries. To use it with what you're trying to do put the keywords in a text file, read in the contents of that text file (file_get_contents()), then explode() it into an array and use array_unique on it. Good luck!
     
    KevinJB, Aug 10, 2006 IP
    klown likes this.
  6. klown

    klown Peon

    Messages:
    2,093
    Likes Received:
    115
    Best Answers:
    0
    Trophy Points:
    0
    #6
    shit didnt know about that.. it kicks ass :)
     
    klown, Aug 10, 2006 IP
  7. KevinJB

    KevinJB Peon

    Messages:
    170
    Likes Received:
    5
    Best Answers:
    0
    Trophy Points:
    0
    #7
    PHP is such a great language to play around with because it comes with so many goodies... One time I was working on a large project involving mucho arrays and just read every single array function in the manual. You have no idea how much candy they give you until you do that :D

    end tangent
     
    KevinJB, Aug 10, 2006 IP
  8. klown

    klown Peon

    Messages:
    2,093
    Likes Received:
    115
    Best Answers:
    0
    Trophy Points:
    0
    #8
    lol i know the feeling, its why i'm a php addict. My java skills have gone to shit cuz of the ease of php programming.
     
    klown, Aug 10, 2006 IP