我试过以下代码。它有时会忽略一些关键和价值。任何人都可以知道它为什么会发生。
$test= array(rand(0, 9)=>"a",rand(0, 9)=>"b",rand(0, 9)=>"c",rand(0, 9)=>"d",rand(0, 9)=>"e");
print_r($test);
答案 0 :(得分:1)
您用另一个值覆盖一个值。例如,如果第一个rand()将创建数字4的键,最后一个rand()也将绘制4,则第一个值将被覆盖为last。
示例:
$test= array(4=>"a",1=>"b",5=>"c",2=>"d",4=>"e");
print_r($test);
将打印:
array(
4 => "e"
1 => "b"
5 => "c"
2 => "d"
)
答案 1 :(得分:0)
也许你想要这样的东西:
$array = ["a","b","c","d","e"];
shuffle($array);
echo '<pre>';
print_r($array);
echo '</pre>';
输出:
//Random
["b,"a","e","c","d"] //possibility 1
["e,"a","b","d","c"] //possibility 2
...
...
etc
注意:每次重新加载页面时,数组元素都是随机的。