avoiding duplicate array elements

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Robert

    avoiding duplicate array elements

    Im a beginner in PHP and Im having a problem with this code. Im
    trying to remove duplicate elements from an array created via $_GET.
    I want users to be able to click on a link which sends an email
    address to an array. I just want to remove duplicate email addresses
    from the array. Ive tried array_unique() on my test server but it
    doesnt work. So i tried to remove duplicates myself before storing
    them into the array. The script works great without the checking, but
    I cant have duplicate emails in the array...please help!

    session_start() ;

    $email = $HTTP_GET_VARS['email'];
    if (isset($_SESSIO N['Array']))
    {

    $Array = $_SESSION['Array'];
    $numElements = count($Array);
    for($counter=0; $counter < $numElements; $counter++)
    {
    /* Problem is here */ if ($Array[$counter] == $email)
    {
    exit();
    }

    else
    {
    array_push($_SE SSION['Array'],$email);
    $EmailArray = $_SESSION['Array'];
    $_SESSION['Array'] = $EmailArray;
    }
    }
    }



    else
    {
    $EmailArray = array();
    array_push($Ema ilArray,$email) ;
    $_SESSION['Array'] = $EmailArray;
    }
  • Nikolai Chuvakhin

    #2
    Re: avoiding duplicate array elements

    gojuka@si.rr.co m (Robert) wrote in message
    news:<742c30b4. 0308201839.5ae5 94c9@posting.go ogle.com>...[color=blue]
    >
    > Im a beginner in PHP and Im having a problem with this code. Im
    > trying to remove duplicate elements from an array created via $_GET.[/color]

    I think you are dealing with the problem at the wrong end. Rather
    than remove duplicates, try not to allow them. There is a function
    called in_array(), which allows you to check if a variable is already
    in array. So you could do something like this:

    if (!in_array ($address, $addresses)) {
    $addresses[] = $address;
    }

    Cheers,
    NC

    Comment

    • Robert

      #3
      Re: avoiding duplicate array elements

      nc@iname.com (Nikolai Chuvakhin) wrote in message news:<32d7a63c. 0308202103.1d39 f3ae@posting.go ogle.com>...[color=blue]
      > gojuka@si.rr.co m (Robert) wrote in message
      > news:<742c30b4. 0308201839.5ae5 94c9@posting.go ogle.com>...[color=green]
      > >
      > > Im a beginner in PHP and Im having a problem with this code. Im
      > > trying to remove duplicate elements from an array created via $_GET.[/color]
      >
      > I think you are dealing with the problem at the wrong end. Rather
      > than remove duplicates, try not to allow them. There is a function
      > called in_array(), which allows you to check if a variable is already
      > in array. So you could do something like this:
      >
      > if (!in_array ($address, $addresses)) {
      > $addresses[] = $address;
      > }
      >
      > Cheers,
      > NC[/color]


      Thanks alot...That solved my problem!
      Rob

      Comment

      • Kevin Thorpe

        #4
        Re: avoiding duplicate array elements

        Robert wrote:[color=blue]
        > Im a beginner in PHP and Im having a problem with this code. Im
        > trying to remove duplicate elements from an array created via $_GET.
        > I want users to be able to click on a link which sends an email
        > address to an array. I just want to remove duplicate email addresses
        > from the array. Ive tried array_unique() on my test server but it
        > doesnt work. So i tried to remove duplicates myself before storing
        > them into the array. The script works great without the checking, but
        > I cant have duplicate emails in the array...please help![/color]

        If you are stuck with an array with duplicates...

        $unique_array = array_keys(arra y_flip($array)) ;

        Only works for numerically indexed arrays of integers or strings.



        Comment

        • jkjr
          New Member
          • Jun 2006
          • 1

          #5
          $unique_array = array_keys(arra y_flip($array)) ;
          Thank you for the above solution, that is quite an elegant solution. After spending about four hours trying to come up with a solution I am quite humbled by your amazing solution. I tried it and it works perfectly.

          I think you are dealing with the problem at the wrong end. Rather
          than remove duplicates, try not to allow them. There is a function
          called in_array(), which allows you to check if a variable is already
          in array. So you could do something like this
          That is very good advice to check for duplicates I will kept this in mind also but I had arrays I needed to remove duplicates from that were already made.

          I just want to thank everyone for their posts that have been so helpful.

          Comment

          Working...