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

时间:2017-04-02 16:11:28

标签: 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'];
    }       
}

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

  

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

我认为这很奇怪,因为我确定它是一个数组,因为代码按预期工作。我还使用了echo gettype($atts);并返回一个数组。当我var_dump($atts);时,它呈现完整的数组。

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

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

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

2 个答案:

答案 0 :(得分:0)

如果条件如下,则更改您:

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

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

答案 1 :(得分:-3)

只需编写以下代码行:

 array_key_exists('customtextcolor', (array) $atts);

这将cast $atts - 变量放入数组中,如果它还不是数组的话。

您还可以检查是否存在具有每个值的数组:

isset($atts['customtextcolor']);