[英]WordPress Plugin PHP: random choice from a list in a variable
我確信 PHP 有辦法處理這個問題,我一直在努力尋找或弄清楚。 我敢肯定這對其他人來說很簡單明了:
我正在為我的 WordPress 插件開發一個新的 function。 我想允許用戶保存一個單詞列表(在 wp-options 中),插件會隨機抽取一個來使用。
當此代碼從一個簡單列表中提取時,如下所示:
$mc6397gh_Rand = array(One,Two,Three,Four);
它將 select 並使用其中一個詞。
但是當我替換(因為我需要)變量時,即使內容完全相同,如下所示:
$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
只有一個key到select from。
您可以執行以下任一操作
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,
) );
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.