How to search array for duplicates using a single array?

Posted by patrick on Stack Overflow See other posts from Stack Overflow or by patrick
Published on 2010-04-28T14:42:44Z Indexed on 2010/04/28 15:13 UTC
Read the original article Hit count: 216

Filed under:

I am checking a list of 10 spots, each spot w/ 3 top users to see if a user is in an array before echoing and storing.

foreach($top_10['top_10'] as $top10)        //loop each spot
{
    $getuser = ltrim($top10['url'], " users/" );   //strip url

if ($usercount < 3) {
      if ((array_search($getuser, $array) !== true)) {

            echo $getuser;                              
            $array[$c++] = $getuser;

        }
        else {

            echo "duplicate <br /><br />";
        }

     }
    }

The problem I am having is that for every loop, it creates a multi-dimensional array for some reason which only allows array_search to search the current array and not all combined. I am wanting to store everything in the same $array. This is what I see after a print_r($array)

Array ( [0] => 120728 [1] => 205247 ) Array ( [0] => 232123 [1] => 091928 )

© Stack Overflow or respective owner

Related posts about php