从数组中选择五个唯一的随机PHP值,并将它们放在单独的变量中

从数组中选择五个唯一的随机PHP值,并将它们放在单独的变量中

问题描述:

I have an array, for example:

 array("aaa", "bbb", "ccc", "ddd", "eee", "fff", "ggg");

I want to select five random and unique values from it and put them in five different variables, for example:

    $one = "ccc"; 
    $two = "aaa";
    $three = "bbb"; 
    $four = "ggg";
    $five = "ddd";

I have already found this code below which works for generating the random strings and just displaying them, but the output I want is for getting them in different variables and being able to use them separately.

<?php

$arr = $arr_history = array("aaa", "bbb", "ccc", "ddd", "eee", "fff", "ggg");

for ( $i = 1; $i < 5; $i++ )
{
  // If the history array is empty, re-populate it.
  if ( empty($arr_history) )
    $arr_history = $arr;

  // Randomize the array.
  array_rand($arr_history);

  // Select the last value from the array.
  $selected = array_pop($arr_history);

  // Echo the selected value.
  echo $selected . PHP_EOL;
 }

我有一个数组,例如: p>

  array(  “aaa”,“bbb”,“ccc”,“ddd”,“eee”,“fff”,“ggg”); 
  code>  pre> 
 
 

我想选择 五个随机且唯一的值,并将它们放在五个不同的变量中,例如: p>

  $ one =“ccc”;  
 $ two =“aaa”; 
 $ three =“bbb”;  
 $ four =“ggg”; 
 $ five =“ddd”; 
  code>  pre> 
 
 

我已经在下面找到了这个代码,用于生成随机字符串和 只是显示它们,但我想要的输出是让它们在不同的变量中,并能够单独使用它们。 p>

 &lt;?php 
 
 $ arr = $ arr_history = array(“aaa”,“bbb”,“ccc”,“ddd”,“eee”,  “fff”,“ggg”); 
 
for($ i = 1; $ i&lt; 5; $ i ++)
 {
 //如果历史数组为空,请重新填充它。
如果 (empty($ arr_history))
 $ arr_history = $ arr; 
 
 // //随机化数组。
 array_rand($ arr_history); 
 
 //从数组中选择最后一个值。
 $  selected = array_pop($ arr_history); 
 
 //回显所选值。
 echo $ selected。  PHP_EOL; 
} 
  code>  pre> 
  div>

You can shuffle the array and use list to assign the values

$arr = array("aaa", "bbb", "ccc", "ddd", "eee", "fff", "ggg");

shuffle( $arr );
list($one, $two, $three, $four, $five) = $arr;

Doc: shuffle(), list()

This should work:

    $arr = array("aaa", "bbb", "ccc", "ddd", "eee", "fff", "ggg");

    $tmp = $arr;
    array_rand($tmp);

    $one = $tmp[0];
    $two = $tmp[1];
    ...

Remeber though that it will not chech if the value in $tmp[n] actually exists

Use this:

$arr = $arr_history = array("aaa", "bbb", "ccc", "ddd", "eee", "fff", "ggg");
$random = [];

for ( $i = 1; $i <= 5; $i++ )
{
  // If the history array is empty, re-populate it.
  if ( empty($arr_history) )
    $arr_history = $arr;

  // Randomize the array.
  array_rand($arr_history);

  // Select the last value from the array.
  $selected = array_pop($arr_history);

  array_push($random, $selected);
}

var_dump($random);
  • I've fixed your loop logic so it now displays 5 items instead of 4.
  • I am using the short syntax for defining an array which requires 5.4 or above.

Output

array(5) {
  [0]=>
  string(3) "ggg"
  [1]=>
  string(3) "fff"
  [2]=>
  string(3) "eee"
  [3]=>
  string(3) "ddd"
  [4]=>
  string(3) "ccc"
}

Live Example

Repl

You can use the shuffle function of PHP to randomizes the order of the elements in the array, then take the first elements.

$randomArray = array("aaa", "bbb", "ccc", "ddd", "eee", "fff", "ggg");
shuffle($randomArray);

$randomArray = array_slice($randomArray, 0, 5);

$randomArray[0]; //1st element
$randomArray[1]; //2nd element
$randomArray[2]; //3rd element...