Home >Backend Development >PHP Tutorial >How to determine if an element is in a known array in PHP
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. |
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.