You can't jam an array of keys (square peg) into a parameter expected to be an index (round hole).
var_export(array_rand($jokes,count($jokes)-1));
Will output something like:
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,
)
What you can do is:
$rand_keys=array_rand($jokes,sizeof($jokes)-1);
var_export($rand_keys);
$rand_jokes=array_intersect_key($jokes,array_flip($rand_keys));
echo "
",json_encode($rand_jokes);
This will output (notice the preserved keys):
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."}
If you don't need to preserve the keys, you can use shuffle()
then collect the full array, bar the last one.
shuffle($jokes);
array_pop($jokes);
echo json_encode($jokes);
Outputs something like:
["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..."]
Late Edit: I failed to acknowledge your use case.
If you just want to output a single random joke use:
echo $jokes[array_rand($jokes)];
By not specifying the second/optional parameter of array_rand()
it will return a single key. Otherwise, if you ask it to return more than one key, it will return an array of keys. Your second parameter count()-1
works on a 2-element array, because you are only asking for 1 key in return (2 - 1); when handling a larger $jokes
array you will have problems with your original method.