简体   繁体   中英

Plugin Error - array_merge(): Argument #2 is not an array

I am building my first wordpress plugin and the error log is throwing up the following errors.

[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

For the errors on line 49 & 50 the code is:

    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 );
}

For the errors on lines 323 & 324 the code is:

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] );
                }
            }
        }
    }
}

I can't seem to figure it out so if anyonecanhelp or point me in the right direction it would be greatly appreciated.

first you need to check your both variables is array

with is_array() like if(is_array($menu))

if not use type cast to convert variable in array

 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);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM