开发者

If Array include in PHP

I've created an array of cities

$cities = explode("|", $city);

Now I want to search that array to see if it contains a particular city for an if statement. Something like this...

If($cities includes(London)){
    Do code;
}

开发者_开发问答How can I do this?


if (in_array('London', $cities)) {
    [...]
}


Just use in_array http://php.net/manual/en/function.in-array.php

Example:

<?php
$os = array("Mac", "NT", "Irix", "Linux");
if (in_array("Irix", $os)) {
    echo "Got Irix";
}
if (in_array("mac", $os)) {
    echo "Got mac";
}
?>


You can use the function in_array():

if (in_array("London", $cities)) {
    Do code;
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜