简体   繁体   中英

PHP, Check if a value is in an array

This is probably very easy but I cannot get it to work in php.
What I need is the following (written explanatory)

if ( 11 is in array(1,3,4,6,7,8,9,11,34,45,56,77) ) : return true;

Thanks a lot :)

$answer = in_array($number,$array);

$answer是布尔值。

Try:

if (in_array(11, $your_array)) {}

See: PHP's in_array() which has a method signature of:

bool in_array ( mixed $needle , array $haystack [, bool $strict = FALSE ] )

The $needle being the value you are looking for, in this instance 11 and the $haystack being the array that you want to search. If you pass true for the final parameter, you are telling PHP to only use the type that you've specified in $needle .

For instance, if you pass "11" and set $strict to true , it would not find 11 .

This'll do it!

$input = array(1,3,4,6,7,8,9,11,34,45,56,77);

$output = array_filter($input, function($var) {
    return ($var == 11);}
);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM