【问题标题】:WordPress Plugin PHP: random choice from a list in a variableWordPress Plugin PHP:从变量列表中随机选择
【发布时间】:2021-05-21 15:45:47
【问题描述】:

我确信 PHP 有办法处理这个问题,但我一直在努力寻找或弄清楚。我相信这对其他人来说很简单明了:

我正在为我的 WordPress 插件开发一个新功能。我想让用户保存一个单词列表(在 wp-options 中),插件会随机抽取一个来使用。

当这段代码从一个简单的列表中提取出来时,像这样:

$mc6397gh_Rand = array(One,Two,Three,Four);

它将选择并使用其中一个词。

但是当我替换(因为我需要)变量时,即使内容完全相同,如下所示:

$mc6397gh_Rand = array($mc6397gh_PerList);

它不会选择一个单词,它只会显示整个列表。我需要它随机选择从 WordPress 选项返回的列表中的一个单词。它需要像查看简单列表一样查看它。

非常感谢任何帮助!

function mc6397gh_replace( $mc6397gh_wp_admin_bar ) {
$mc6397gh_my_account=$mc6397gh_wp_admin_bar->get_node('my-account');
$mc6397gh_PerList = get_option(mc6397gh_MyList) ;

    $mc6397gh_Rand = array($mc6397gh_PerList);
    $mc6397gh_RandIndex = array_rand($mc6397gh_Rand);
    $mc6397gh_PerRand = $mc6397gh_Rand[$mc6397gh_RandIndex];

    $mc6397gh_newtitle = str_replace( 'Howdy,', $mc6397gh_PerRand, $mc6397gh_my_account->title );
    $mc6397gh_wp_admin_bar->add_node( array(
    'id' => 'my-account',
    'title' => $mc6397gh_newtitle,
) );

【问题讨论】:

    标签: php wordpress variables random plugins


    【解决方案1】:

    问题出在一行

    $mc6397gh_Rand = array($mc6397gh_PerList);
    

    当您执行此类操作时,变量$mc6397gh_Rand 将类似于[0 => $mc6397gh_PerList]。所以array_rand只有一个键可供选择。

    您可以执行以下任一操作

    function mc6397gh_replace( $mc6397gh_wp_admin_bar )
        {
            $mc6397gh_my_account = $mc6397gh_wp_admin_bar->get_node('my-account');
            $mc6397gh_PerList = get_option(mc6397gh_MyList);
    
            $mc6397gh_Rand = $mc6397gh_PerList;   ## Changed Line
            $mc6397gh_RandIndex = array_rand($mc6397gh_Rand);
            $mc6397gh_PerRand = $mc6397gh_Rand[$mc6397gh_RandIndex];
    
            $mc6397gh_newtitle = str_replace('Howdy,', $mc6397gh_PerRand, $mc6397gh_my_account->title);
            $mc6397gh_wp_admin_bar->add_node(array(
                'id' => 'my-account',
                'title' => $mc6397gh_newtitle,
            ));
        }
    

    或者你可以这样做

    function mc6397gh_replace($mc6397gh_wp_admin_bar)
        {
            $mc6397gh_my_account = $mc6397gh_wp_admin_bar->get_node('my-account');
            $mc6397gh_PerList = get_option(mc6397gh_MyList);
    
            $mc6397gh_Rand = array($mc6397gh_PerList);
            $mc6397gh_RandIndex = array_rand($mc6397gh_Rand[0]);    ## Changed Line
            $mc6397gh_PerRand = $mc6397gh_Rand[$mc6397gh_RandIndex];
    
            $mc6397gh_newtitle = str_replace('Howdy,', $mc6397gh_PerRand, $mc6397gh_my_account->title);
            $mc6397gh_wp_admin_bar->add_node(array(
                'id' => 'my-account',
                'title' => $mc6397gh_newtitle,
            ));
        }
    

    话虽如此,我不知道为什么你可以简单地完成$mc6397gh_RandIndex = array_rand($mc6397gh_PerList);

    【讨论】:

      【解决方案2】:

      感谢您周到的回答。尽管如此,它还是行不通。我花了很多时间来解决这两个答案,试图找到它出错的地方。

      (我相信其中有几个额外的步骤,因为我在谷歌和实验中找到了答案。)

      当我使用第一个答案时,返回总是 0,或者实际上是一个圆圈。

      对于第二个,什么都没有显示——没有任何单词出现。

      在第一个答案中,如果我做

      $mc6397gh_Rand = array(one,two,three);
      

      它有效,但没有使用我在变量中返回的单词列表。

      在第二个中,如果我这样做并且还消除了 [0],它仍然可以工作,但变量中没有我需要的单词列表。

      当在变量中返回相同的列表时,到目前为止没有任何效果。在我的原文中,我看到了存储在变量中的整个单词列表。

      有没有办法让变量中的列表像数组一样工作?

      【讨论】:

      • 你指的第一个和第二个答案是什么?您也可以在“单词列表变量”和$mc6397gh_Rand 上执行 var_dump() 或 print_r 并在此处发布输出。
      【解决方案3】:

      Google 的奇迹终于出现了。要将变量中包含的列表转换为数组,请使用“eval”。请参阅下面隔离的线。这行得通!

          if (get_option('mc6397gh_Type') == 'personalrandom') {
      
          function mc6397gh_replace( $mc6397gh_wp_admin_bar ) {
          $mc6397gh_my_account=$mc6397gh_wp_admin_bar->get_node('my-account');
          $mc6397gh_PerList = get_option(mc6397gh_MyList) ;
      
          eval("\$mc6397Create = array($mc6397gh_PerList);");
      
          $mc6397gh_RandIndex = array_rand($mc6397Create);
          $mc6397gh_PerRand = $mc6397Create[$mc6397gh_RandIndex];
      
              $mc6397gh_newtitle = str_replace( 'Howdy,', $mc6397gh_PerRand, $mc6397gh_my_account->title );
              $mc6397gh_wp_admin_bar->add_node( array(
              'id' => 'my-account',
              'title' => $mc6397gh_newtitle,
          ) );
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-07-03
        • 2020-04-06
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多