php:如果url包含一个数组值,那么X.

时间:2014-08-19 18:41:15

标签: php arrays

我会重新解释整个情况,因为它感到困惑。请记住,我对编码知之甚少。 我有一个这样的数组:

<?php
$mytestarray = array(
'test1'=>'aaa',
'test2'=>'aaa',
'test3'=>'ccc'
);

现在我需要检查数据键是否存在于网页的当前网址中。如果它们存在,我会做一些事情,如果不是其余代码将继续加载(此代码将放在我的网站的标题中)。因此,如果此语句返回false,则需要运行许多代码。

所以在上面的数组示例中 http://localhost/test1xxx.php应返回True,因为url包含test1,这是第一个数组键 http://localhost/test2asdasdasdas.php也应该返回True。

就是这样。我不需要更多或更少或不同的东西。由于数组值将在其他地方使用,脚本应检查URL中是否存在数组键

1 个答案:

答案 0 :(得分:4)

修改
改进了用户的答案。

<?php
$mytestarray = array(
"test1"=>'aaa',
"test2"=>'bbb',
"test3"=>'ccc'
);
// Create a function so it's cooler
function in_URI(array $array)
{
    // Please review the code entirely.
    foreach($array AS $key => $value){
        // If the URI does contain one of the words in the array
        if (strpos($_SERVER['REQUEST_URI'], $key) !== FALSE) {
            // Return that it has been found
            return true;
        }
    }
    // Return that the values have not been found
    return false;
}

if(in_URI($mytestarray)){

    echo "YAAAY! It's in here!";

}else{

    echo "Awwww :(";
}