I have an array of 30 values and I need to extract from this array 3 different random values. How can I do it?
Share
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Shamelessly stolen from the PHP manual:
https://www.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.