SitePoint Sponsor

User Tag List

Results 1 to 9 of 9

Thread: Array Help

  1. #1
    SitePoint Addict
    Join Date
    Nov 2002
    Location
    Maryland
    Posts
    307
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Array Help

    I need to be able to determine if an integer occurs more than once in an array.

    So with the following array if we tested 9 then it would return false.

    PHP Code:
    $test = array(555225669); 
    What is the best way to do this?

    Thanks

  2. #2
    SitePoint Zealot
    Join Date
    Dec 2006
    Posts
    182
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Here's one way, someone may have a better way:
    PHP Code:
    function has_duplicates($array$value) {
        
    $count array_count_values($array);
        return (
    $count[$value] >1) ? true false;
    }

    $test = array(555225669); 

    if (
    has_duplicates($test,9)) {
        echo 
    "duplicates found!";
    } else {
        echo 
    "no duplicates found!";


  3. #3
    SitePoint Addict
    Join Date
    Nov 2002
    Location
    Maryland
    Posts
    307
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks funkdaddy! That works!

  4. #4
    SitePoint Guru risoknop's Avatar
    Join Date
    Feb 2008
    Location
    end($world)
    Posts
    834
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Longer and possibly less efficient solution. Sorry, I'm bored

    PHP Code:
    function hasDuplicates($array$item) {
        
    $count 0;
        foreach (
    $array as $value) {
            if (
    $count 1) {
                return 
    true;
            }
            else if (
    $value == $item) {
                
    $count++;
            }
        }
        if (
    $count 1) {
            return 
    true;
        } else {
            return 
    false;
        }
    }

    $test = array(555225699); 

    echo (
    hasDuplicates($test6)) ? 'has duplicates' 'no duplicates'

  5. #5
    SitePoint Addict
    Join Date
    Jul 2008
    Posts
    213
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Another way to do it using array_unique.

    PHP Code:
    $test = array(555225669);
    $test_unique array_unique($test);

    if (
    count($test) == count($test_unique)) {
      print 
    'There are no duplicates.';
    } else {
      print 
    'There are duplicates.';


  6. #6
    . shoooo... silver trophy logic_earth's Avatar
    Join Date
    Oct 2005
    Location
    CA
    Posts
    9,013
    Mentioned
    8 Post(s)
    Tagged
    0 Thread(s)
    Logic without the fatal effects.
    All code snippets are licensed under WTFPL.


  7. #7
    @php.net Salathe's Avatar
    Join Date
    Dec 2004
    Location
    Edinburgh
    Posts
    1,397
    Mentioned
    65 Post(s)
    Tagged
    0 Thread(s)
    Another option would be to use array_keys with the optional $search_value argument.

    has_duplicates function using array_keys
    PHP Code:
    // Returns TRUE when $value appears in $array
    // more than once, FALSE otherwise.
    // Note: Use optional third argument ($strict)
    //       to force type check on the $value.
    function has_duplicates($array$value$strict TRUE)
    {
        return (bool) (
    count(array_keys($array$value$strict)) > 1);

    Salathe
    Software Developer and PHP Manual Author.

  8. #8
    SitePoint Wizard bronze trophy
    Join Date
    Jul 2008
    Posts
    5,757
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Salathe, why the unnecesary boolean cast and parenthesis? For self documentation/clarity?

  9. #9
    @php.net Salathe's Avatar
    Join Date
    Dec 2004
    Location
    Edinburgh
    Posts
    1,397
    Mentioned
    65 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by crmalibu View Post
    Salathe, why the unnecesary boolean cast and parenthesis? For self documentation/clarity?
    I often write return (bool) just so it is completely clear at-a-glance: especially in this kind of case where the line starts with return count otherwise.
    Salathe
    Software Developer and PHP Manual Author.


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
  •