WordPress插件PHP:从变量的列表中随机选择



我确信PHP有办法处理这个问题,我一直在努力寻找或解决它。我相信这对其他人来说是简单而明显的:

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

当这个代码从一个简单的列表中提取时,比如:

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

问题出在行

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

感谢您周到的回答。尽管如此,它还是不起作用。我花了很多时间研究这两个答案,试图找到它的出发点。

(我相信,当我在谷歌上搜索并尝试寻找答案时,还有几个额外的步骤。(

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

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

在第一个答案中,如果我制作

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

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

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

到目前为止,当在变量中返回相同的列表时,一切都不起作用。在我的原件中,我看到了存储在变量中的整个单词列表。

有没有一种方法可以使变量中的列表作为数组工作?

谷歌的奇迹终于出现了。要将包含在变量中的列表转换为数组,请使用";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,
) );
}

最新更新