[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now


Randomly select x elements from an array

Posted on 2010-11-22
Medium Priority
Last Modified: 2012-05-10
I have an array, like:
$array = array('dog','cat','sam','puppy','mouse','coke');

How can I create a new array, that has only x number of elements, randomly chosen from $array?
Question by:interclubs
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
LVL 20

Expert Comment

ID: 34190086

$x = 3;
$array = array('dog','cat','sam','puppy','mouse','coke');
$rand_keys = array_rand($array , $x);
echo $input[$rand_keys[0]] . "\n";
echo $input[$rand_keys[1]] . "\n";
echo $input[$rand_keys[3]] . "\n";
LVL 13

Accepted Solution

dsmile earned 2000 total points
ID: 34190281
Use shuffle() to make array randomly ordered, and then pick up x element(s) to create new array by using array_splice()
$array = array('dog','cat','sam','puppy','mouse','coke');

//reorder the array randomly

//create new array
$n = 3; // change this to x number you want
$newarray = array_splice($array, $n);


Open in new window


Author Closing Comment

ID: 34190497
LVL 20

Expert Comment

ID: 34190561
Just curious for my own future reference...what was wrong with my solution?

Author Comment

ID: 34190606
It didn't work. I think you meant:

$x = 3;
$array = array('dog','cat','sam','puppy','mouse','coke');
$rand_keys = array_rand($array , $x);
echo $array[$rand_keys[0]] . "\n";
echo $array[$rand_keys[1]] . "\n";
echo $array[$rand_keys[2]] . "\n";

(Changed parts in bold). That made it work, but it still wasn't giving me a new array, it was just randomly picking elements from an array.

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Build an array called $myWeek which will hold the array elements Today, Yesterday and then builds up the rest of the week by the name of the day going back 1 week.   (CODE) (CODE) Then you just need to pass your date to the function. If i…
Originally, this post was published on Monitis Blog, you can check it here . In business circles, we sometimes hear that today is the “age of the customer.” And so it is. Thanks to the enormous advances over the past few years in consumer techno…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …
Suggested Courses

656 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question