-1

当我认为我有一个数组时,我有一个致命错误声称我有一个字符串:

Fatal error: [] operator not supported for strings in /Applications/MAMP/htdocs/tankards_wordpress/wp-content/themes/tankardsleague/functions.php on line 566

还有以下警告: Warning: in_array() expects parameter 2 to be array, string given in /Applications/MAMP/htdocs/tankards_wordpress/wp-content/themes/tankardsleague/functions.php on line 579

我猜我在某处有语法问题?我以为我已经初始化了变量,但也许我错过了一个?我会欣赏一些更有经验的眼睛看看。

function forum_subscribe_member_player()
{
    global $user_ID;
    $players= get_users();

    foreach($players as $player)
    {
        $user_info = get_userdata($player->ID);
        $playeremail = $user_info->user_email;

        if(!empty($playeremail) && user_can( $player-> ID, 'contributor'))
        {                   
            $list = get_option('mf_forum_subscribers_1', array());


            if( is_player_subscribed($player->ID)) //remove player if already exists (user clicked unsubscribe)
            {
                $key = array_search($playeremail, $list);
                unset($list[$key]);
            }
            else
                $list[] = $playeremail;
            update_option('mf_forum_subscribers_1', $list);
        }
    }       
}

function is_player_subscribed($user_ID)
{
    if($user_ID)
      {
        $useremail = get_userdata($user_ID, 'user_email');

        $list = get_option("mf_forum_subscribers_1", array());
        if(!empty($list) && in_array($useremail, $list))
        {
          return true;
        }
        return false;
      }

}

function call_forum_subscribe_member_player()
{
    forum_subscribe_member_player();
} 

第 566$list[] = $playeremail;行是 第 579 行是if(!empty($list) && in_array($useremail, $list))

4

1 回答 1

0

好吧,您可以将其类型$list转换为数组以确保它始终是数组,即使初始值为 null/empty/etc(基本上是数组以外的任何类型):

$list = (array)get_option('mf_forum_subscribers_1', array());
于 2013-10-06T11:51:10.817 回答