[英]PHP Undefined index - Isset() is false when index is in array
我無法弄清楚這里的問題是什么。 我有一個帶有預定義索引的關聯數組,當試圖訪問其中一個索引時,我得到了未定義的索引錯誤,這是代碼,
if(!isset($score_value[$index])){
echo $index . ' isnt in array: <br/>';
print_r($score_value);
exit;
}
print_r($score_value[$index]);
輸出是這樣的:
pi_cholesterol
不在數組中:
Array (
[pi_overall_health] => Array (
[4] => 4 [1] => 1 [2] => 4 [3] => 1
)
[pi_bmi] => Array (
[Healthy Weight] => 4 [Obese] => 3 [Overweight] => 3
)
[pi_cholesterol] => Array (
[Yes - its level is too high] => 6 [Yes - its level is ok] => 3 [No] => 1
)
)
正如您所看到的, pi_cholesterol
是數組中的索引但由於某種原因, isset()
將其標記為不在數組中,如果我嘗試使用array_key_exists()
則會發生同樣的事情。 我可能會忽略一些簡單的事情,但我看不到它。
歡迎任何建議!
對不起我自己的愚蠢錯誤,$ index被覆蓋在代碼中的另一個地方,並附加了一個尾隨的空格。
Mjh,axiac和rahul_m建議使用trim()幫我解決了這個問題。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.