Home >Backend Development >PHP Tutorial >How to determine if an element is in a known array in PHP

How to determine if an element is in a known array in PHP

WBOY
WBOYOriginal
2016-07-29 08:56:441747browse

In PHP, you can use the in_array() function to directly determine whether an element is in an array. If the element exists in the array, the in_array() function will return true, otherwise it will return false

Syntax

in_array(<em>search</em>,<em>array</em>,<em>type</em>)
Parameters Description
search Required. Specifies the value to search for in the array.
array Required. Specifies the array to search.
type optional. If this parameter is set to true, it is checked whether the type of the searched data and the value of the array are the same.
Note: If the search parameter is a string and the type parameter is set to true, the search is case-sensitive.

Since PHP4.2, the search parameter may now also be an array

For example:

$arr=array("107","网站","工作室");
if(in_array("107",$arr)){
    echo "匹配成功";
}else{
    echo "匹配失败";
}

The running result is:

Match successful


$arr=array("107","网站","工作室");
if(in_array("河南大学",$arr)){
    echo "匹配成功";
}else{
    echo "匹配失败";
}

The running result is:

Matching failed


The above introduces how PHP determines whether an element is in a known array, including the relevant aspects. I hope it will be helpful to friends who are interested in PHP tutorials.

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