How can I check if a value is already in an array?

Discussion in 'PHP' started by raspms, Mar 8, 2012.

  1. #1
    If i wish to check whether a value is already stored in an array or not
    Then how can i chekc that? Is this any function is there in php?
     
    raspms, Mar 8, 2012 IP
  2. krishmk

    krishmk Well-Known Member

    Messages:
    1,376
    Likes Received:
    40
    Best Answers:
    0
    Trophy Points:
    185
    #2
    Use in_array()

    <?php
    $color_array = array("blue", "red", "orange");
    
    if (in_array("black", $color_array)){
      echo("Black exists in color array");
    }
    else{
      echo("Black does not exists in color array");
    }
    ?>
    PHP:
     
    krishmk, Mar 8, 2012 IP
  3. ratnadewi

    ratnadewi Peon

    Messages:
    10
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    that right, use in_array method.
     
    ratnadewi, Mar 8, 2012 IP
  4. deMiracles

    deMiracles Peon

    Messages:
    21
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #4
    in_array , ​ it is :D
     
    deMiracles, Mar 10, 2012 IP
  5. ThePHPMaster

    ThePHPMaster Well-Known Member

    Messages:
    737
    Likes Received:
    52
    Best Answers:
    33
    Trophy Points:
    150
    #5
    in_array is fine for regular sites. However, it is more efficient to use isset. Sometimes much more efficient when you enter into thousands/millions of elements.

    The reason isset is more efficient than is_array is that it does not loop through each element to check if it exists, but rather does direct address check.

    
    <?php
    $colorArray = array('blue', 'red', 'orange');
    
    if ( isset($colorArray['black']) ) {
      echo 'Black exists in color array';
    } else {
      echo 'Black does not exists in color array';
    }
    
    
    PHP:
     
    ThePHPMaster, Mar 10, 2012 IP
  6. Alex Roxon

    Alex Roxon Active Member

    Messages:
    424
    Likes Received:
    11
    Best Answers:
    7
    Trophy Points:
    80
    #6
    The code you've posted isn't relevant at all. To borrow from php.net:



    So in this instance, if using on an array, you'd basically be checking if a value exists for an array key. isset($colorArray['blue']) would return false, as it isn't set - though isset($colorArray[0]) would return true, since $colorArray[0] is set, and it has a value of 'blue'.

     
    Alex Roxon, Mar 10, 2012 IP
  7. furqanartists

    furqanartists Well-Known Member

    Messages:
    35
    Likes Received:
    0
    Best Answers:
    1
    Trophy Points:
    101
    #7
    Here are some working examples: php.net/in_array
     
    furqanartists, Mar 11, 2012 IP
  8. ThePHPMaster

    ThePHPMaster Well-Known Member

    Messages:
    737
    Likes Received:
    52
    Best Answers:
    33
    Trophy Points:
    150
    #8
    Yep, you are correct. I was thinking of array_key_exists, not in_array.

     
    ThePHPMaster, Mar 11, 2012 IP