如果阵列包括PHP
问题描述:
我创建城市如果阵列包括PHP
$cities = explode("|", $city);
现在我想搜索该阵列,看它是否包含特定城市的if语句的数组。像这样...
If($cities includes(London)){
Do code;
}
我该怎么做?
答
if (in_array('London', $cities)) {
[...]
}
答
只需使用in_array
http://php.net/manual/en/function.in-array.php
例子:
<?php
$os = array("Mac", "NT", "Irix", "Linux");
if (in_array("Irix", $os)) {
echo "Got Irix";
}
if (in_array("mac", $os)) {
echo "Got mac";
}
?>