Php 如何从textbox中的值生成数组元素的数量
我有一个文本框: 阵列大小: 在此之后,我有按钮显示数组:Php 如何从textbox中的值生成数组元素的数量,php,Php,我有一个文本框: 阵列大小: 在此之后,我有按钮显示数组: <html> <body> <form name="form" method="post"> Size of array: <input type="number"value="5" name="size", min="2", max="10"> <input type="submit" name="Se
<html>
<body>
<form name="form" method="post">
Size of array: <input type="number"value="5" name="size", min="2", max="10">
<input type="submit" name="Send" value="Show array"></span></p>
</form>
</body>
</html>
<?php
if(isset($_POST['Send']))
{
//what must be here?
}
?>
阵列大小:
但我不知道如何从文本框中输入的数字生成随机数,例如,如果文本框包含8,则从2到10生成8个随机数。您能帮忙吗?编辑新版本我想这就是您想要的吗
$numb=range(2,10);
shuffle($numb);
$output = array_slice($numb, 0, $_POST['guess']);
echo implode(',',$output);
range()shuffle
将其随机化,array\u slice()
根据表单输入选择元素的数量<代码>内爆
显示
旧版本:
<html>
<body>
<form name="form" method="post">
Size of array: <input name="guess" type="number" value="5" name="size", min="2", max="10">
<input type="submit" name="Send" value="Show array"></span></p>
</form>
</body>
</html>
<?php
if(isset($_POST['Send']))
{
for ($i = 0; $i < $_POST['guess']; $i ++) {
$random[] = rand(2, 10);
}
echo implode(',', $random);
}
?>
阵列大小:
for
循环正在运行用户选择的次数,rand
正在选取这些数字,内爆
是将数字粘在一起作为输出首先,您的表单没有输入来添加要添加的数字posted@IdontDownVote现在会更好吗?很好,但输入也需要一个名称
值,然后我猜rand()
,但我不确定“2到10之间的8个随机数”是什么“2,3,4,5,6,7,8,9,10”以随机顺序?或者2-10之间的8个数字允许重复?@IdontDownVote不能重复。它也可以是2,8,4,5,3,6,7,9如果我输入4,那么它会生成4个随机数,从2到10(5,3,7,6)$numb=range(size)代码>我有这一串代码..现在无法检查,因为主机没有加载,idk为什么!当它可用时,我将检查您的答案!:-)如果我需要使用var_dump怎么办?然后使用它<代码>变量转储($output)代码>?我需要输出格式int(4)int(7)int(6)int(2)
all ok。。。。。。。。。。。