doutu1939
doutu1939
2017-04-12 11:15

为什么array_rand()使用两个元素,但不是更多,或者更少?

  • random
  • php
  • arrays
已采纳

I'm randomly trying to select a text string from an array. I have a single dimension array with the same kind of structure as the following:

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

And I'm selecting items from it using:

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

If I only have two items in the array then it randomizes perfectly however as soon as I go over or under that number nothing returns.

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享
  • 邀请回答

1条回答

  • dttvb115151 dttvb115151 4年前

    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.

    点赞 评论 复制链接分享