tags:

views:

637

answers:

5

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

+2  A: 

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]);
 }
pheelicks
sounds good. how can I remove the array element?
creativz
`unset($array[$key])`
Franz
`unset($my_array[$random]);`
thetaiko
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
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
+5  A: 

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.
middus
+1 More elegant
Franz
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.
Joey
That was not part of the spec ;).
middus
A: 

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

adam
This won't work if 2 elements are the same
pheelicks
@pheelicks It should work because array_rand returns an index and not the element itself.
middus
D'oh. Sorry adam
pheelicks
It's not the most graceful solution (my money is on middus) but it does work. Any comments/reasons from the downvoters?
adam
+4  A: 

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];
}
VolkerK
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
@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
I don't actually know. I have not tested it myself.
middus
A: 
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))));
chris