tags:

views:

30

answers:

4
$array = array('a', 'b', 'c', 'd', /*... letters from 3 alphabets*/);

$letter = 'some symbol, posted by user'; // real length = 1

How to get know, is $letter one of the symbols, listed in $array?

Like, if $letter = 'G' and there is no G in $array, well then return false.


Yep, I tried in_array(), but there are too many symbols, is there any other (shorter) solution?

+4  A: 

in_array() http://ca.php.net/in_array

if(in_array($letter,$array)) {
  // your code
}

Another method would be to do this

// THIS WAY
$array = array('a','b','c'); // and continue this way. 
$array = array_flip($array);

// OR THIS
$array = array('a'=>0,'b'=>0,'c'=>0);

// This will stay the same
if($array[$letter] != null) {
  // your code
}
Angelo R.
You can turn the letters array into keys with `array_flip()` instead.
BoltClock
Thanks BoltClock, I've never run into a need for this, but it might come in handy. I've updated my answer to include your method as well.
Angelo R.
It should be `$array = array_flip($array);` as that function does not modify the parameter. (Nitpicking, I know.)
BoltClock
Totally valid though, someone might stumble into this and end up posting about it again because it didn't work.
Angelo R.
A: 
$IsInArray = in_array($letter, $array); //RETURNS TRUE OR FALSE
d2burke
A: 

Check the in_array() function ... lets you find a needle (single letter) in a haystack (an array)

Coach John
A: 

You could use a string instead of an array:

$letters = 'abcdefghi...';
$letter = 'a';

if (false !== strpos($letters, $letter)) {
    // valid letter
}
nikic