Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    Regular Coder
    Join Date
    Dec 2006
    Posts
    306
    Thanks
    24
    Thanked 0 Times in 0 Posts

    get rid off the repeat word from an array

    Hi;

    How could i get rid off the repeat words from an array? Thanks

    eg;

    $a[]="the , think , this , that , the , their , that , the , the , those";

    i just want the array reduce to "the, think, that,those".

  • #2
    Regular Coder
    Join Date
    Jul 2010
    Location
    Oregon City
    Posts
    280
    Thanks
    5
    Thanked 50 Times in 49 Posts
    use array_unique() function

  • Users who have thanked Adee for this post:

    kamkam (11-12-2011)

  • #3
    Supreme Overlord Spookster's Avatar
    Join Date
    May 2002
    Location
    Marion, IA USA
    Posts
    6,280
    Thanks
    4
    Thanked 83 Times in 82 Posts
    Use above. But if you were to do this the hard way you could do something like this:

    PHP Code:
    <?php
    $words 
    = array('the''think''this''that''the''their''that''the''the''those');
    $uniqueWords = array();
    foreach (
    $words as $word) {
       if (!
    in_array($word$uniqueWords)) {
          
    $uniqueWords[] = $word;
       }
    }  
    var_dump($uniqueWords);
    ?>
    Spookster
    CodingForums Supreme Overlord
    All Hail Spookster

  • Users who have thanked Spookster for this post:

    kamkam (11-12-2011)

  • #4
    Regular Coder
    Join Date
    Dec 2006
    Posts
    306
    Thanks
    24
    Thanked 0 Times in 0 Posts
    Thanks a lot


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •