2012-06-11 93 views
-2
<?php 
$whitelist = array('contact', 'about', 'user'); 
$_GET['page'] = array('contact'); 
$test = $_GET['page']; 
if(isset($test)) 
{ 
    if(in_array($whitelist, $test)) 
    { 
    $got = $test; 
    echo $got; 
    } 
    else 
    { 
    $got = 'home'; 
    echo $got; 
    } 
} 
?> 

現在,在這裏,我應該得到'聯繫'的結果,但我得到'家'。這是爲什麼 ?無法理解in_array()

+0

它看起來像你試圖找到一個數組的數組,嘗試將其更改爲$測試=「接觸」 –

回答

3

in_array in_array第一個參數應該是needle(意思是:你在找什麼),第二個參數應該是haystack(意思是:我們正在尋找的地方)。

我認爲你逆轉這些,以及needl應該是字符串(或其他變量類型),但不是數組。

所以你的腳本應該是這樣的:

<?php 
$whitelist = array('contact', 'about', 'user'); 
$test = 'contact'; 
if(isset($test)) 
{ 
    if(in_array($test, $whitelist)) 
    { 
    $got = $test; 
    echo $got; 
    } 
    else 
    { 
    $got = 'home'; 
    echo $got; 
    } 
} 
?> 
+0

查看以下鏈接什麼Repox回答我http://stackoverflow.com/questions/10978798/ how-to-locate-to-a-default-location-on-incorrect-get-value-using-headers –

+0

這是提供信息和正確的選擇 –

3

因爲白名單是一個字符串數組和$ _GET [「頁」]是一個數組,而不是字符串。而且你有錯誤的參數。

+0

檢查以下鏈接什麼Repox回答我的http://stackoverflow.com/問題/ 10978798 /如何到定位到一個默認-位置上不正確,獲取價值,使用報頭 –

0
<?php 
$whitelist = array('contact', 'about', 'user'); 
$_GET['page'] = 'contact'; 
$test = $_GET['page']; 
if(isset($test)) 
{ 
    if(in_array($test, $whitelist)) 
    { 
    $got = $test; 
    echo $got; 
    } 
    else 
    { 
    $got = 'home'; 
    echo $got; 
    } 
} 
?>