我有以下名为 $fruits
的数组:
Array
(
[response] => Array
(
[errormessage] => banana
)
[blah] => Array
(
[blah1] => blahblah1
[blah2] => blahblah2
[blah3] => blahblah3
[blah4] => blahblah4
)
)
然而当我这样做时:
isset($fruits['response']['errormessage']['orange'])
它返回真!
究竟是什么会导致这种奇怪的行为,我该如何解决?
谢谢!
最佳答案
归结为 PHP 的疯狂类型系统。
$fruits['response']['errormessage']
是字符串 'banana'
,因此您正试图通过['orange']
索引。
字符串 'orange'
为了索引的目的被转换为一个整数,所以它变成了 0
,如 $fruits['response'] ['errormessage'][0]
。字符串的第 0 个索引是字符串的第一个字符,因此对于非空字符串,它实际上是已设置的。因此 isset()
返回 true。
我一开始就不知道您要做什么,所以我无法为此提供任何“修复”。这是设计使然。
关于php - isset() 为不存在的数组键返回 true 的奇怪行为,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9132715/