dtdfl62844 2014-07-07 09:05
浏览 181
已采纳

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

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.

  • 写回答

1条回答 默认 最新

  • dtbi27903 2014-07-07 09:09
    关注

    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);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!
  • ¥15 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?
  • ¥15 求daily translation(DT)偏差订正方法的代码
  • ¥15 js调用html页面需要隐藏某个按钮
  • ¥15 ads仿真结果在圆图上是怎么读数的
  • ¥20 Cotex M3的调试和程序执行方式是什么样的?
  • ¥20 java项目连接sqlserver时报ssl相关错误
  • ¥15 一道python难题3
  • ¥15 牛顿斯科特系数表表示