Php生成随机和不同的数字

时间:2012-11-07 21:25:44

标签: php random

我在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,如果重新加载脚本其他组合

感谢问候!!!

2 个答案:

答案 0 :(得分:2)

您忘记了$中的count($products)。因此,解析器将其视为字符串"products",其count()为1.因此,rand()函数返回零或一,在原始数组中对应于"1""2"

答案 1 :(得分:0)

尝试使用shuffle

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

Demo