对于不存在的数组键,isset() 返回 true 的奇怪行为 [英] Strange behavior with isset() returning true for an Array Key that does NOT exist

查看:44
本文介绍了对于不存在的数组键,isset() 返回 true 的奇怪行为的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个名为 $fruits 的数组:

I have the following array called $fruits:

Array
(
    [response] => Array
        (
            [errormessage] => banana
        )  

    [blah] => Array
        (
            [blah1] => blahblah1
            [blah2] => blahblah2
            [blah3] => blahblah3
            [blah4] => blahblah4
        )  

)

然而当我这样做时:

isset($fruits['response']['errormessage']['orange'])

它返回

到底是什么会导致如此奇怪的行为,我该如何解决?

What on earth would cause such a strange behavior and how can I fix this?

谢谢!

推荐答案

[n] 也是一种访问字符串中字符的方法:

[n] is also a way to access characters in a string:

$fruits['response']['errormessage']['orange']
==
$fruits['response']['errormessage'][0] // cast to int
==
b (the first character, at position 0) of 'banana'

使用 array_key_exists,可能与 is_array 结合使用.

Use array_key_exists, possibly in combination with is_array.

这篇关于对于不存在的数组键,isset() 返回 true 的奇怪行为的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆