Home >Backend Development >PHP Problem >How to determine if a value is in an array in php

How to determine if a value is in an array in php

王林
王林Original
2020-04-27 15:33:494365browse

How to determine if a value is in an array in php

There are three methods:

1. Use the in_array() function

in_array($value,$array)
注意:in_array('','',true)还有第三个参数,为true时还会判断数据类型

2. First use the array_flip() function to key the array Invert the value, and then make a judgment

if(isset($array[$value])){
}

3. First convert the array into a string, and then use the strpos function for further processing

public static function inArray($item, $array) {
    $str = implode(',', $array);
    $str = ',' . $str . ',';
    $item = ',' . $item . ',';
    return false !== strpos($item, $str) ? true : false;
}

For more related tutorials, please visit php中文网.

The above is the detailed content of How to determine if a value is in an array in php. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn