Php 生成10个随机数
我试图从一个数字数组中显示10个随机数,但没有成功 这是我的代码:Php 生成10个随机数,php,Php,我试图从一个数字数组中显示10个随机数,但没有成功 这是我的代码: <?php $num = array("2000", "2001", "2002", "2003", "2004", "2005", "2006", "2007", "2008", "2009"); //echo $num[rand(0,9)]; echo '<br/>'; for ($num = 2000; $num <= 10; $num[rand(0,9)]++) { echo "The
<?php
$num = array("2000", "2001", "2002", "2003", "2004", "2005", "2006", "2007", "2008", "2009");
//echo $num[rand(0,9)];
echo '<br/>';
for ($num = 2000; $num <= 10; $num[rand(0,9)]++)
{
echo "The number is " . $num . "<br />";
}
?>
虽然我的ubuntu php.ini上有display\u errors=On
,但脚本没有显示任何内容
我哪里出错了?只需使用:
之后,您可以使用foreach
对这些进行迭代:
$rand = array_rand($num, 10);
foreach($rand as $key) {
echo "The number is " . $num[key] . "<br />";
}
$rand=array\u rand($num,10);
foreach(兰特作为$key){
echo“数字为”。$num[键]。”
;
}
从$num=2000
开始循环,并在$num
我会改用函数
<?php
$num = array("2000", "2001", "2002", "2003", "2004", "2005", "2006", "2007", "2008", "2009");
shuffle($num);
echo '<br/>';
foreach ($num as $value)
{
echo "The number is " . $value . "<br />";
}
?>
试试看
$num=array(“2000”、“2001”、“2002”、“2003”、“2004”、“2005”、“2006”、“2007”、“2008”,
"2009");
洗牌($num);
//echo$num[rand(0,9)];
回声“
”;
对于($i=0;$i ”;
}
$num=数组(“2000”、“2001”、“2002”、“2003”、“2004”、“2005”、“2006”、“2007”、“2008”、“2009”);
对于($i=0;$i),他应该使用foreach
提供英文链接:)注意,shuffle
更改($i=0;$i)的源数组
<?php
$num= array("2000", "2001", "2002", "2003", "2004", "2005", "2006", "2007", "2008",
"2009");
//echo $num[rand(0,9)];
echo '<br/>';
for ($i = 0; $i < count($num); $i++)
{
echo "The number is " . $num[rand(0, count($num))] . "<br />";
}
?>
<?php
$num = array("2000", "2001", "2002", "2003", "2004", "2005", "2006", "2007", "2008", "2009");
shuffle($num);
echo '<br/>';
foreach ($num as $value)
{
echo "The number is " . $value . "<br />";
}
?>
$num= array("2000", "2001", "2002", "2003", "2004", "2005", "2006", "2007", "2008",
"2009");
shuffle($num);
//echo $num[rand(0,9)];
echo '<br/>';
for ($i = 0; $i < count($num); $i++)
{
echo "The number is " . $num[$i] . "<br />";
}
$num= array("2000", "2001", "2002", "2003", "2004", "2005", "2006", "2007", "2008", "2009");
for($i=0; $i<10; $i++){
$rand = rand(0,count($num));
echo "The number is " . $num[$rand] . "<br />";
// unset($num[$rand]); //to get unique numbers each time .. this will unset array item after its showed
// $num = array_values($num); //reindex after unset
}