array_key_exists() 期望参数 2 为数组,给定字符串

Ins*_*ple -2 php arrays

我有这段代码:

if( array_key_exists('customtextcolor',$atts) ){
    // If in array, then check if none, if not none, add to CSS classes
    if ( 'none' !== $atts['customtextcolor'] ) {
        $custstyles[] = $atts['customtextcolor'];
    }       
}
Run Code Online (Sandbox Code Playgroud)

这是第 90 行,我的php.log在第 90 行显示错误。

Apr 2 08:46:16 #####.net ool www: PHP 警告: array_key_exists() 期望参数 2 为数组,在 /var/www/####/wp-content/plugins/Fusion 中给出的字符串-Builder-Custom-Text-For-HP/fusion-builder-custom-text-for-hp.php 第 90 行

我觉得这很奇怪,因为我确信它是一个数组,因为代码按预期工作。我也使用过echo gettype($atts);,它返回一个数组。当我var_dump($atts);它呈现完整的数组。

知道 php.log 显示此错误消息吗?

如果$atts是字符串,为什么将gettype()其识别为数组?为什么要把var_dump()它渲染为数组?

我查看了array_key_exists()StackOverflow 上的其他问题,但据我所知,它们不是同一个问题。

Imr*_*ran 6

更改 if 条件如下:

if(is_array($atts) && array_key_exists('customtextcolor', $atts))

这将首先检查是否$atts是一个数组,然后检查array_key_exist操作。