我试图使用以下代码来检查当前URL是否在数组中.
$reactfulPages = array(
'url-one',
'url-two',
'url-three',
);
if (strpos($url, $reactfulPages) == true) {
echo "URL is inside list";
}
Run Code Online (Sandbox Code Playgroud)
我认为我设置数组的方式不正确,因为以下代码(检查一个URL)工作正常..
if (strpos($url,'url-one') == true) { // Check if URL contains "landing-page"
}
Run Code Online (Sandbox Code Playgroud)
谁能帮我吗?
数组很好,要检查的功能不正确.该strpos()功能用于检查字符串位置.
检查数组中是否存在某些内容的正确方法可以使用该in_array()功能.
<?php
$reactfulPages = array(
'url-one',
'url-two',
'url-three',
);
if(in_array($url, $reactfulPages)) {
echo "The URL is in the array!";
// Continue
}else{
echo "The URL doesn't exists in the array.";
}
?>
Run Code Online (Sandbox Code Playgroud)
我希望这对你有用.