Strange behavior with isset() returning true for an Array Key that does NOT exist

前端 未结 3 1933
臣服心动
臣服心动 2020-12-12 02:05

I have the following array called $fruits:

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

            


        
3条回答
  •  南方客
    南方客 (楼主)
    2020-12-12 02:45

    It just boils down to PHP's crazy type system.

    $fruits['response']['errormessage'] is the string 'banana', so you're attempting to access a character in that string by the ['orange'] index.

    The string 'orange' is converted to an integer for the purposes of indexing, so it becomes 0, as in $fruits['response']['errormessage'][0]. The 0th index of a string is the first character of the string, so for non-empty strings it's essentially set. Thus isset() returns true.

    I don't know what you're trying to do in the first place so I can't offer any "fix" for this. It's by design.

提交回复
热议问题