vote up 1 vote down star

From an array

 $my_array = array('a','b','c','d','e');

I want to get two DIFFERENT random elements.

With the following code:

 for ($i=0; $i<2; $i++) {
    $random = array_rand($my_array);  # one random array element number
    $get_it = $my_array[$random];    # get the letter from the array
    echo $get_it;
 }

it is possible to get two times the same letter. I need to prevent this. I want to get always two different array elements. Can somebody tell me how to do that? Thanks

flag

63% accept rate

5 Answers

vote up 2 vote down check

You could always remove the element that you selected the first time round, then you wouldn't pick it again. If you don't want to modify the array create a copy.

 for ($i=0; $i<2; $i++) {
    $random = array_rand($my_array);  # one random array element number
    $get_it = $my_array[$random];    # get the letter from the array
    echo $get_it;

    unset($my_array[$random]);
 }
link|flag
sounds good. how can I remove the array element? – creativz Feb 24 at 15:53
unset($array[$key]) – Franz Feb 24 at 15:54
unset($my_array[$random]); – thetaiko Feb 24 at 15:54
Why use a loop? Why not just get the first random value, remove it from the array then get the second? You don't need to remove the second. – adam Feb 24 at 15:58
I used the loop because it meant minimal changes to creativz's code. Plus it makes it easy to extend the code to pick an arbitrary number of elements – pheelicks Feb 24 at 16:02
vote up 5 vote down

What about this?

$random = $my_array; // make a copy of the array
shuffle($random); // randomize the order
echo array_pop($random); // take the last element and remove it
echo array_pop($random); // s.a.
link|flag
+1 More elegant – Franz Feb 24 at 15:55
For very large arrays this can be pretty slow, though as you certainly don't have to shuffle the complete array just to get two elements. – Johannes Rössel Feb 24 at 15:56
That was not part of the spec ;). – middus Feb 24 at 16:04
vote up 4 vote down

array_rand() can take two parameters, the array and the number of (different) elements you want to pick.

mixed array_rand ( array $input [, int $num_req = 1 ] )
$my_array = array('a','b','c','d','e');
foreach( array_rand($my_array, 2) as $key ) {
  echo $my_array[$key];
}
link|flag
I like this one. However, according to the comments on php.net the order of the returned indices is not so random. +1 anyway ;) – middus Feb 24 at 16:02
@middus: is this still true for recent versions of php? There have been a number of complaints about some random functions, esp. on win32. I thought they have been (somewhat) fixed. – VolkerK Feb 24 at 16:07
I don't actually know. I have not tested it myself. – middus Feb 24 at 16:28
vote up 0 vote down
foreach (array_intersect_key($arr, array_flip(array_rand($arr, 2))) as $k => $v) {
    echo "$k:$v\n";
}

//or

list($a, $b) = array_values(array_intersect_key($arr, array_flip(array_rand($arr, 2))));
link|flag
vote up -1 vote down

Get the first random, then use a do..while loop to get the second:

$random1 = array_rand($my_array);
do {
    $random2 = array_rand($my_array);
} while($random1 == $random2);

This will keep looping until random2 is not the same as random1

link|flag
This won't work if 2 elements are the same – pheelicks Feb 24 at 15:58
@pheelicks It should work because array_rand returns an index and not the element itself. – middus Feb 24 at 16:25
D'oh. Sorry adam – pheelicks Feb 24 at 16:45
It's not the most graceful solution (my money is on middus) but it does work. Any comments/reasons from the downvoters? – adam Feb 24 at 17:03

Your Answer

Get an OpenID
or
never shown

Not the answer you're looking for? Browse other questions tagged or ask your own question.