14

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

creativz
  • 10,369
  • 13
  • 38
  • 35

8 Answers8

19

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
  • 95,432
  • 20
  • 163
  • 226
  • 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 '10 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 '10 at 16:07
  • I don't actually know. I have not tested it myself. – middus Feb 24 '10 at 16:28
9

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
  • 9,103
  • 1
  • 31
  • 33
7

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
  • 7,461
  • 2
  • 45
  • 50
  • 1
    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 Hopkinson Feb 24 '10 at 15:58
  • 2
    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 '10 at 16:02
4

You can shuffle and then pick a slice of two. Use another variable if you want to keep the original array intact.

$your_array=[1,2,3,4,5,6,7];
shuffle($your_array); // randomize the order
$your_array = array_slice($your_array, 0, 2); //pick 2
Selay
  • 6,024
  • 2
  • 27
  • 23
3
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))));
goat
  • 31,486
  • 7
  • 73
  • 96
1

here's a simple function I use for pulling multiple random elements from an array.

function get_random_elements( $array, $limit=0 ){

    shuffle($array);

    if ( $limit > 0 ) {
        $array = array_splice($array, 0, $limit);
    }

    return $array;
}
jbrahy
  • 4,228
  • 1
  • 42
  • 54
0

Here's how I did it. Hopefully this helps anyone confused.

$originalArray = array( 'first', 'second', 'third', 'fourth' );
$newArray= $originalArray;
shuffle( $newArray);
for ($i=0; $i<2; $i++) {
  echo $newArray[$i];
}
-1

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 Hopkinson
  • 28,281
  • 7
  • 65
  • 99