[英]PHP Undefined index - Isset() is false when index is in array
I'm not able to figure out what the issue is here. 我无法弄清楚这里的问题是什么。 I have an associative array with predefined indexes and when trying to access one of the indexes, I'm getting the undefined index error, here's the code,
我有一个带有预定义索引的关联数组,当试图访问其中一个索引时,我得到了未定义的索引错误,这是代码,
if(!isset($score_value[$index])){
echo $index . ' isnt in array: <br/>';
print_r($score_value);
exit;
}
print_r($score_value[$index]);
The output was this: 输出是这样的:
pi_cholesterol
isn't in array: 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
)
)
As you can see pi_cholesterol
is an index in the array but for some reason isset()
is flagging it as not being in the array, same thing happens if I try with array_key_exists()
. 正如您所看到的,
pi_cholesterol
是数组中的索引但由于某种原因, isset()
将其标记为不在数组中,如果我尝试使用array_key_exists()
则会发生同样的事情。 There might be some simple thing I'm overlooking but I can't see it. 我可能会忽略一些简单的事情,但我看不到它。
Any suggestions welcome! 欢迎任何建议!
Sorry my own stupid mistake, $index was being overwritten in another place in the code and was being appended with a trailing whitespace. 对不起我自己的愚蠢错误,$ index被覆盖在代码中的另一个地方,并附加了一个尾随的空格。
Mjh, axiac and rahul_m suggesting to use trim() helped me solve this. Mjh,axiac和rahul_m建议使用trim()帮我解决了这个问题。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.