简体   繁体   English

php获取两个不同的随机数组元素

[英]php get two different random array elements

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 谢谢

array_rand() can take two parameters, the array and the number of (different) elements you want to pick. array_rand()可以采用两个参数,即数组和要选择的(不同)元素的数量。

mixed array_rand ( array $input [, int $num_req = 1 ] ) 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];
}

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.

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]);
 }
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))));

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;
}

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];
}

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

Get the first random, then use a do..while loop to get the second: 获取第一个随机,然后使用do..while循环获取第二个:

$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 这将保持循环,直到random2与random1不同

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM