Solved

Randomly select x elements from an array

Posted on 2010-11-22
5
445 Views
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?
0
Comment
Question by:interclubs
  • 2
  • 2
5 Comments
 
LVL 20

Expert Comment

by:DVation191
ID: 34190086
http://php.net/manual/en/function.array-rand.php

$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";
0
 
LVL 13

Accepted Solution

by:
dsmile earned 500 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()
<?php
$array = array('dog','cat','sam','puppy','mouse','coke');

//reorder the array randomly
shuffle($array);

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

print_r($newarray);

Open in new window

0
 

Author Closing Comment

by:interclubs
ID: 34190497
nice.
0
 
LVL 20

Expert Comment

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

Author Comment

by:interclubs
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.
0

Featured Post

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

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

Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
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 look for a specific file type in a local or remote server directory using PHP.

776 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