isset arrays

Discussion in 'PHP' started by Weirfire, Oct 10, 2005.

  1. #1
    I'm trying to find out if a variable exists in an array.

    So far I've tried using isset($a,$b) to find out if $a exists in the array $b but I get the same result every time under both circumstances.

    I also tried using array_key_exists($a, $b) which also returned the same result.

    Any ideas how I can find out if a variable exists within an array?
     
    Weirfire, Oct 10, 2005 IP
  2. someonewhois

    someonewhois Peon

    Messages:
    177
    Likes Received:
    14
    Best Answers:
    0
    Trophy Points:
    0
    #2
    You want in_array. Array_key_exists is the same as going isset($array[$key]).
     
    someonewhois, Oct 10, 2005 IP
    Weirfire likes this.
  3. Weirfire

    Weirfire Language Translation Company

    Messages:
    6,979
    Likes Received:
    365
    Best Answers:
    0
    Trophy Points:
    280
    #3
    With in_array($a, $b) you can enter a third value which seems to be boolean. Does this mean you can run this command so that

    if(in_array($a,$b,false)){
    print "This was false";}


    would return

    This was false

    if a was not in b?
     
    Weirfire, Oct 10, 2005 IP
  4. digitalpoint

    digitalpoint Overlord of no one Staff

    Messages:
    38,334
    Likes Received:
    2,613
    Best Answers:
    462
    Trophy Points:
    710
    Digital Goods:
    29
    #4
    No, the third parameter defaults to FALSE. It's used to also check the data type.
    What do you mean by a variable existing in an array? The variable being a key in an array or an actual value?
     
    digitalpoint, Oct 10, 2005 IP
  5. Weirfire

    Weirfire Language Translation Company

    Messages:
    6,979
    Likes Received:
    365
    Best Answers:
    0
    Trophy Points:
    280
    #5
    The actual value.

    in_array seems to do the trick. For some reason I was under the impression that in_array searches for the pattern rather than an identical match.
     
    Weirfire, Oct 10, 2005 IP