Search for a specific character inside an array

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

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

How to find out if there is $letterone of the characters listed in $array?

For example, if $letter = 'G'not Gin $array, well then return false.


Yes, I tried in_array(), but there are too many characters, is there another (shorter) solution?

+3
source share
4 answers

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
}
+4
source
$IsInArray = in_array($letter, $array); //RETURNS TRUE OR FALSE
0
source

in_array()... ( ) ()

0

:

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

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

Source: https://habr.com/ru/post/1762917/


All Articles