开发者

If Array include in PHP

开发者 https://www.devze.com 2023-04-06 18:21 出处:网络
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...

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

精彩评论

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