我有个数组里面是[a,b,c]
随机获取一个值后,如a
下次数组只剩[b,c]
再从b,c里取,取完为止,求代码
<?php
$input = array("a", "b", "c", "d", "e");
shuffle($input);
$i = 1;
foreach ($input as $item) {
echo "第".strval($i)."次取出".$item." ";
}
?>
<?php
$arr = [3, 4, 7, 8, 9];
function getNumber(&$arr)
{
$len = count($arr);
if ($len > 0) {
$index = mt_rand(0, $len - 1);
$value = $arr[$index];
array_splice($arr, $index, 1);
return $value;
}
return null;
}
while (true) {
$value = getNumber($arr);
if (null === $value) {
break;
}
echo $value . "\n";
}
$data = ['a', 'b', 'c', 'd', 'e'];
while ($data && ($key = array_rand($data)) !== null) {
var_dump($data[$key]);
unset($data[$key]);
}