我正在随机尝试从数组中选择一个文本字符串。我有一个单维数组,其结构与以下相同:
$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 :(得分: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
数组时,您的原始方法会出现问题。