SitePoint Sponsor

User Tag List

Results 1 to 13 of 13
  1. #1
    SitePoint Guru Husain's Avatar
    Join Date
    Sep 2001
    Posts
    620
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Unique array values

    I have an array that looks something like this:
    Code:
    $Participant[1]['Name'] = 'Husain';
    $Participant[1]['Email'] = 'some@email.com';
    
    $Participant[2]['Name'] = 'Someone';
    $Participant[2]['Email'] = 'some@email.com';
    I want some ideas regarding how I could check if there are no duplicate emails in my array. I don't want to remove duplicate email addresses, just return an error if there are any.

    The size of the array is pre-determined.

    Hope to get some bright ideas

  2. #2
    Web-coding NINJA! silver trophy beetle's Avatar
    Join Date
    Jul 2002
    Location
    Dallas, TX
    Posts
    2,900
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    This seems to work...not too hard....
    PHP Code:
    $foundEmails = array();
    for( 
    $i 0; ( $email $Participant[$i]['Email'] ); $i++ )
    {
        if ( !
    in_array$email$foundEmails ) )
            
    $foundEmails[] = $email;
        else
            die( 
    $email " is a duplicate" );
    }
    echo( 
    "All emails unique" );
    ?> 
    beetle a.k.a. Peter Bailey
    blogs: php | prophp | security | design | zen | software
    refs: dhtml | gecko | prototype | phpdocs | unicode | charsets
    tools: ide | ftp | regex | ffdev




  3. #3
    SitePoint Guru Husain's Avatar
    Join Date
    Sep 2001
    Posts
    620
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    That is smart! Thanks.

  4. #4
    Non-Member
    Join Date
    Jan 2003
    Posts
    5,748
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    There is in fact a quicker and easier way of doing this... there is a PHP Array function to check for duplicates within an array.

    Have a look at the manual; see what functions are there - one single function call and your problem is solved.

  5. #5
    Web-coding NINJA! silver trophy beetle's Avatar
    Join Date
    Jul 2002
    Location
    Dallas, TX
    Posts
    2,900
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Dr Livingston
    There is in fact a quicker and easier way of doing this... there is a PHP Array function to check for duplicates within an array.

    Have a look at the manual; see what functions are there - one single function call and your problem is solved.
    I believe that. Memorizing all those functions is hard!
    beetle a.k.a. Peter Bailey
    blogs: php | prophp | security | design | zen | software
    refs: dhtml | gecko | prototype | phpdocs | unicode | charsets
    tools: ide | ftp | regex | ffdev




  6. #6
    Sidewalking anode's Avatar
    Join Date
    Mar 2001
    Location
    Philadelphia, US
    Posts
    2,205
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    That's why php.net is there. It's always pretty easy to go to the manual and look to see if there's a built-in function you can use; I always check when I run across a problem I havent coded before.
    TuitionFree a free library for the self-taught
    Anode Says... Blogging For Your Pleasure

  7. #7
    Web-coding NINJA! silver trophy beetle's Avatar
    Join Date
    Jul 2002
    Location
    Dallas, TX
    Posts
    2,900
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by anode
    That's why php.net is there. It's always pretty easy to go to the manual and look to see if there's a built-in function you can use; I always check when I run across a problem I havent coded before.
    True, but from what I can tell, there are 59 array functions. Unless the function name just jumps out at you ( like find_duplicates() ) sometimes it can be hard to locate what you need.
    beetle a.k.a. Peter Bailey
    blogs: php | prophp | security | design | zen | software
    refs: dhtml | gecko | prototype | phpdocs | unicode | charsets
    tools: ide | ftp | regex | ffdev




  8. #8
    Sidewalking anode's Avatar
    Join Date
    Mar 2001
    Location
    Philadelphia, US
    Posts
    2,205
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    There's descriptions next to 'em, you know.
    TuitionFree a free library for the self-taught
    Anode Says... Blogging For Your Pleasure

  9. #9
    SitePoint Guru Husain's Avatar
    Join Date
    Sep 2001
    Posts
    620
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Dr Livingston
    There is in fact a quicker and easier way of doing this... there is a PHP Array function to check for duplicates within an array.

    Have a look at the manual; see what functions are there - one single function call and your problem is solved.
    I did go through all the array function before posting this question but I couldn't find any. There is a function called array_unique but that removes duplicates. I do not want to do that. I might have missed something perhaps.

  10. #10
    Web-coding NINJA! silver trophy beetle's Avatar
    Join Date
    Jul 2002
    Location
    Dallas, TX
    Posts
    2,900
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Husain
    I did go through all the array function before posting this question but I couldn't find any. There is a function called array_unique but that removes duplicates. I do not want to do that. I might have missed something perhaps.
    Perhaps Dr. Livingston can impress us
    beetle a.k.a. Peter Bailey
    blogs: php | prophp | security | design | zen | software
    refs: dhtml | gecko | prototype | phpdocs | unicode | charsets
    tools: ide | ftp | regex | ffdev




  11. #11
    Non-Member
    Join Date
    Jan 2003
    Posts
    5,748
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Not attempting to impress anyone - I remembered about the array function from memory only, not knowing the function name, nor that it removed an array element 8)

    Only that it dealt with duplicates. But since you put me up to the challenge, I did have a nosey around the manual, trying to find a way of doing this.

    At the moment, the challenge is still open. LoL

  12. #12
    ********* wombat firepages's Avatar
    Join Date
    Jul 2000
    Location
    Perth Australia
    Posts
    1,717
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    In this case I dont think it gets any better than Beetle's script, you can use a combo of several other array_functions but none alone do the job in this case (as its a multi-dimensional array else array_unique would have been useful)

  13. #13
    Non-Member
    Join Date
    Jan 2003
    Posts
    5,748
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Yep, I was looking for a way - from the manual - off a way to 'catch' the array indexes that were thrown out by the array_unique function.


Bookmarks

Posting Permissions

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