0

我在 php 中创建它以生成随机数

> <?php
> 
> $products=array("1","2","3","4","5","6","7","8","9","10");
> 
> for ($i=0;$i<count($products);$i++) {
> 
> $numbers=rand(0,count(products));
> 
> print "".$products[$numbers]."<br>";
> 
> } ?>

我尝试在 bucle 中生成不同的数字,但总是显示相同的数字 1212121212 仅此而已,我如何生成这个字符串或数组,例如最后显示 2 3 4 1 5 6 7 9 8 10 ,如果重新加载脚本其他组合

感谢和问候 !!!

4

2 回答 2

2

你忘了$输入count($products)。结果,解析器将其视为字符串"products",其 acount()为 1。因此,该rand()函数返回零或一,在原始数组中对应于"1"and "2"

于 2012-11-07T21:28:55.083 回答
0

尝试使用shuffle

$products=array("1","2","3","4","5","6","7","8","9","10"); 
shuffle($products);
foreach($products as $v)
 echo $v; 

演示

于 2012-11-07T21:29:15.977 回答