我有一个数组,其值为1,5,6,9,11,45,56等.我想要做的是随机一个值,也许是6.然后我想选择一个随机值,不包括6 (所以没有双打).然后是一个随机值,不包括最后两个,全部来自数组内部.有帮助吗?我试图在没有while循环的情况下这样做,但如果它们是必要的那么就这样吧.
Chr*_*isJ 10
我建议如下:
# pick a random key in your array
$rand_key = array_rand($your_array);
# extract the corresponding value
$rand_value = $your_array[$rand_key];
# remove the key-value pair from the array
unset($your_array[$rand_key]);
Run Code Online (Sandbox Code Playgroud)
请参阅:array_rand,unset.
首先对阵列进行洗牌,然后将其用作堆栈:
$a = array(1, 5, 6, 9, 11, 45, 56);
shuffle($a);
// now you can have your picks:
$pick = array_pop($a);
$pick = array_pop($a);
$pick = array_pop($a);
$pick = array_pop($a);
...
Run Code Online (Sandbox Code Playgroud)