2017-04-12 101 views
0

我正在隨機嘗試從數組中選擇一個文本字符串。我有同樣的一種結構如下面的一維數組:array_rand()爲什麼使用兩個元素但不多於或少於?

$jokes = array("Why is Peter Pan always flying? He neverlands." , 
       "My girlfriend yelled at me today saying, \"You weren't even listening just now, were you?! I thought, \"Man, what a weird way to start a conversation.\"", 
       "I used to have a job collecting leaves. I was raking it in.", 
       "What's the leading cause of dry skin? Towels.", 
       "I tell you what often gets overlooked - garden fences.", 
       "I wear a stethoscope so that in a medical emergency I can teach people a valuable lesson about assumptions.", 
       "Toasters were the first form of pop-up notifications.", 
       "I love sniffing my F1 key... don't worry though, I'm trying to get help.", 
       "I just ate a frozen apple. Hardcore.", 
       "RIP boiled water. You will be mist.", 
       "Archaeology really is a career in ruins...", 
       "You know what they say about cliffhangers...", 
       "I went out with a girl called Simile, I don't know what I metaphor.", 
       "My server sings, it's a Dell."); 

而且我從它使用選擇項目:

echo json_encode($jokes[array_rand($jokes, count($jokes)-1)]); 

如果我只有兩個項目在數組中,然後它隨機完成,但只要我超過或低於這個數字沒有任何回報。

+0

我不認爲'array_rand'按照你認爲應該的方式工作。你讀過[docs](http://php.net/array_rand)嗎? –

+0

從['array_rand()'](http://php.net/manual/en/function.array-rand.php)的文檔中:當只選擇一個條目時,'array_rand()'返回一個隨機輸入。否則,返回隨機條目的一組鍵。在第二種情況下,$ jokes [array_rand(...)]'和$ jokes ['Array']'是一樣的,顯然它沒有設置。 – axiac

+1

更好地使用'shuffle()'就像'shuffle($笑話); $笑話= array_values($笑話);/*重新索引* /打印$笑話[0];'http://php.net/manual/en/function.shuffle.php – JustOnUnderMillions

回答

1

您不能將一列鍵(方形釘)卡入預期爲索引的參數(圓孔)中。

var_export(array_rand($jokes,count($jokes)-1)); 

將輸出類似:

array (
    0 => 0, 
    1 => 1, 
    2 => 2, 
    3 => 3, 
    4 => 4, 
    5 => 6, 
    6 => 7, 
    7 => 8, 
    8 => 9, 
    9 => 10, 
    10 => 11, 
    11 => 12, 
    12 => 13, 
) 

你可以做的是:

$rand_keys=array_rand($jokes,sizeof($jokes)-1); 
var_export($rand_keys); 
$rand_jokes=array_intersect_key($jokes,array_flip($rand_keys)); 
echo "\n",json_encode($rand_jokes); 

這將輸出(注意保存鍵):

array (
    0 => 0, 
    1 => 1, 
    2 => 2, 
    3 => 3, 
    4 => 4, 
    5 => 5, 
    6 => 6, 
    7 => 8, 
    8 => 9, 
    9 => 10, 
    10 => 11, 
    11 => 12, 
    12 => 13, 
) 
{"0":"Why is Peter Pan always flying? He neverlands.","1":"My girlfriend yelled at me today saying, \"You weren't even listening just now, were you?! I thought, \"Man, what a weird way to start a conversation.\"","2":"I used to have a job collecting leaves. I was raking it in.","3":"What's the leading cause of dry skin? Towels.","4":"I tell you what often gets overlooked - garden fences.","5":"I wear a stethoscope so that in a medical emergency I can teach people a valuable lesson about assumptions.","6":"Toasters were the first form of pop-up notifications.","8":"I just ate a frozen apple. Hardcore.","9":"RIP boiled water. You will be mist.","10":"Archaeology really is a career in ruins...","11":"You know what they say about cliffhangers...","12":"I went out with a girl called Simile, I don't know what I metaphor.","13":"My server sings, it's a Dell."} 

如果你不需要保存鑰匙,你可以使用shuffle()然後收集完整的數組,然後禁用最後一個數組。

shuffle($jokes); 
array_pop($jokes); 
echo json_encode($jokes); 

輸出是這樣的:

["What's the leading cause of dry skin? Towels.","Archaeology really is a career in ruins...","Why is Peter Pan always flying? He neverlands.","I tell you what often gets overlooked - garden fences.","Toasters were the first form of pop-up notifications.","I wear a stethoscope so that in a medical emergency I can teach people a valuable lesson about assumptions.","My girlfriend yelled at me today saying, \"You weren't even listening just now, were you?! I thought, \"Man, what a weird way to start a conversation.\"","My server sings, it's a Dell.","I love sniffing my F1 key... don't worry though, I'm trying to get help.","I used to have a job collecting leaves. I was raking it in.","RIP boiled water. You will be mist.","I just ate a frozen apple. Hardcore.","You know what they say about cliffhangers..."] 

後期編輯:我不承認你的使用情況。

如果你只是想輸出一個亂開玩笑用途:

echo $jokes[array_rand($jokes)]; 

如果不指定的array_rand()第二/可選參數,它將返回一個鍵。否則,如果你要求它返回多個鍵,它將返回一個鍵數組。你的第二個參數count()-1適用於一個2元素數組,因爲你只需要返回1個鍵(2 - 1);當處理更大的$jokes數組時,您將遇到原始方法的問題。

相關問題