插件错误 - array_merge():参数#2不是数组

时间:2014-07-07 09:05:51

标签: php arrays wordpress

我正在构建我的第一个wordpress插件,错误日志会引发以下错误。

[06-Jul-2014 20:07:21 UTC] PHP Warning:  array_merge(): Argument #2 is not an array in \wp-content\plugins\test-plugin\screen.php on line 49

[06-Jul-2014 20:07:21 UTC] PHP Warning:  array_merge(): Argument #2 is not an array in \wp-content\plugins\test-plugin\screen.php on line 50

[06-Jul-2014 20:07:21 UTC] PHP Warning:  end() expects parameter 1 to be array, null given in \wp-content\plugins\test-plugin\screen.php on line 323

[06-Jul-2014 20:07:21 UTC] PHP Warning:  Invalid argument supplied for foreach() in \wp-content\plugins\test-plugin\screen.php on line 324

对于第49行和第49行的错误50代码是:

    function register_settings() {
    global $menu;
    global $submenu;
    $this->menus = array_merge(array(), $menu);
    $this->submenus = array_merge(array(), $submenu);
    $this->settings = get_option( $this->settings_name );
    register_setting( 'admin-theme', $this->settings_name );
}

对于第323行和第3行的错误324代码是:

function admin_menu() {
    global $menu;
    global $submenu;
    // update menu
    end( $menu );
    foreach ($menu as $k=>&$v){
        $id = explode(' <span', $v[0]);
        $slug = 'menu_'.strtolower( str_replace( ' ','_',$id[0] ) );
        $slug_hide = $slug.'_hide';
        if($id[0] != NULL && $this->get_setting($slug) !== NULL){
            $v[0] = $this->get_setting($slug). ( isset($id[1]) ? ' <span '.$id[1] : '' );
        }
        if( $this->get_setting($slug_hide) ){
            unset($menu[$k]);
        }
        // update the submenu
        if( isset($submenu[$v[2]]) ){
            foreach ($submenu[$v[2]] as $key=>&$val){               
                $id = explode(' <span', $val[0]);
                $slug_sub = $slug.'_'.strtolower( str_replace( ' ','_',$id[0] ) );
                $slug_sub_hide = $slug_sub.'_hide';
                if($id[0] != NULL && $this->get_setting($slug_sub) !== NULL){
                    $val[0] = $this->get_setting($slug_sub). ( isset($id[1]) ? ' <span '.$id[1] : '' );
                }
                if( $this->get_setting($slug_sub_hide) ){                       
                    unset( $submenu[$v[2]][$key] );
                }
            }
        }
    }
}

我似乎无法弄清楚如果有人帮助或指出我正确的方向,我们将不胜感激。

1 个答案:

答案 0 :(得分:3)

首先你需要检查你的两个变量是否为数组

is_array()

if(is_array($menu))

如果不使用类型转换来转换数组中的变量

 if(is_array($menu))
      $this->menus = array_merge(array(), $menu);
    else 
      $this->menus = array_merge(array(), (array)$menu);
    if(is_array($submenu))
      $this->submenus = array_merge(array(), $submenu);
    else
      $this->submenus = array_merge(array(), (array)$submenu);