Home > Backend Development > PHP Tutorial > php怎么判断是否在数组中存在

php怎么判断是否在数组中存在

WBOY
Release: 2016-06-13 12:56:37
Original
1184 people have browsed it

php如何判断是否在数组中存在
有一个数组:
array
(
[0] =>a
[1] =>b
[2] =>c
[3] =>d
)
我想做一个判断,比如判断一个已知字符串a是否存在于这个数组中,如果存在就执行一条语句。


------解决方案--------------------
in_array()
------解决方案--------------------
引用:
引用:in_array($a, $array)
如果写成IF语句,那是怎样写,是
if(in_array($a, $array)==true)
{

}
吗?

in_array返回bool值
if(in_array($a, $array)){<br />
}
Copy after login


唉,老毛病又犯了!!!

------解决方案--------------------
引用:
引用:in_array($a, $array)
如果写成IF语句,那是怎样写,是
if(in_array($a, $array)==true)
{

}
吗?


你的写法不是最佳写法。

测试存在这样写:
if(in_array($a, $array))<br />
{<br />
<br />
}
Copy after login


测试不存在这样写:
if(!in_array($a, $array))<br />
{<br />
<br />
}
Copy after login
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