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

王林
Release: 2023-02-28 20:52:01
Original
4320 people have browsed it

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时还会判断数据类型
Copy after login

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

if(isset($array[$value])){
}
Copy after login

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;
}
Copy after login

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!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template