iT邦幫忙

0

[php]詢問in_array找不到get的特定值(已解決)

舜~ 2019-05-20 12:00:071563 瀏覽

$_GET 是一個全域的陣列
in_array(...)可以判斷特定值是否在特定陣列內

我想知道URL參數中是否有debug三數,所以用inarray('debug',$GET)來判斷,
但結果竟然是false...

想問一下各位大大為什麼會這樣嗎?


網址:updepdata.php?ps_id=1000835&menutype=updepdata&debug=1

var_dump(boolToString($_GET['debug']).';'.boolToString(in_array('debug',$_GET)));

var_dump($_GET);

function boolToString($bit){
    return $bit?1:0;
}

輸出:
string(5) "1;0"
array(4) {
["ps_id"]=> string(7) "1000835"
["menutype"]=> string(9) "updepdata"
["debug"]=> string(1) "1"
}

圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

2 個回答

1
Han
iT邦研究生 1 級 ‧ 2019-05-20 12:12:56
最佳解答
in_array
(PHP 4, PHP 5, PHP 7)
in_array — Checks if a value exists in an array

https://php.net/manual/en/function.in-array.php

這個function是找array的value
你要找的是array的key


應該要用

array_key_exists
(PHP 4 >= 4.0.7, PHP 5, PHP 7)
array_key_exists — Checks if the given key or index exists in the array

https://www.php.net/manual/en/function.array-key-exists.php

請參考以上

1

你是否搞錯key的應用及value的應用。

in_array是針對你的陣列值來做in判斷處理。
依照你所輸出的
array(4) {
["ps_id"]=> string(7) "1000835"
["menutype"]=> string(9) "updepdata"
["debug"]=> string(1) "1"
}
其值並未包到有debug字串存在的值。當然會給你false了。
如果要確定你的key = debug存不存在。
一般是用isset或是上面跟你說的array_key_exists這兩種函式來處理的。

我要發表回答

立即登入回答