在If函数中使用in_array时出错

时间:2019-07-05 17:59:15

标签: php arrays

使用if(array_key_exists)函数时遇到错误

我的详细信息:

$projectid = array( "9gag", "facebook", "google" );

我正在将Value从GET传递给projectid。

case 1 : url.com?projectid = google , $_SESSION['projectid'] = google
case 1 : url.com?projectid = 9gag , $_SESSION['projectid'] = 9gag
case 3 : url.com?projectid = koko , $_SESSION['projectid'] = koko

即使koko不存在于数组中,但$ _SESSION ['projectid'] = koko。为什么会发生此错误?

当我想要$ _SESSION ['projectid'] = msp2

if(!isset($_SESSION['projectid'])) // If session is not set then redirect to Login Page
{
    $_SESSION['projectid'] =  "default";
}

else if(isset($_SESSION['projectid']) && isset($_GET['projectid']))

{
    if(array_key_exists($_GET['projectid'] ,$projectid ) !== false)
    {
        $_SESSION['projectid'] = $_GET['projectid'];
    }

    else $_SESSION['projectid'] = "msp2" ;

}

我得到的结果

var_dump($projectid); = array(3) { [0]=> string(14) "9gag" [1]=> string(14) "facebook" [2]=> string(14) "google" }
var_dump($_GET['projectid']); = string(4) "koko"

得到了解决方案:将array_search替换为array_key_exists

0 个答案:

没有答案