Choose 3 different random values from an array

前端 未结 3 1469
小蘑菇
小蘑菇 2020-12-18 18:17

I have an array of 30 values and I need to extract from this array 3 different random values. How can I do it?

3条回答
  •  清酒与你
    2020-12-18 18:55

    Shamelessly stolen from the PHP manual:

    
    

    http://us2.php.net/array_rand

    Note that, as of PHP 5.2.10, you may want to shuffle (randomize) the keys that are returned via shuffle($rand_keys), otherwise they will always be in order (smallest index first). That is, in the above example, you could get "Neo, Trinity" but never "Trinity, Neo."

    If the order of the random elements is not important, then the above code is sufficient.

提交回复
热议问题