如何随机地从数组中取出例子,将它们洗牌并将它们放入一个新的数组中,以格式化为一个列表 [英] How to randomly take examples from arrays, shuffle them and place them into a new array to be formatted as a list
问题描述
在上一个问题我没有意识到自己无法添加其他更改,所以这里是我正在使用的。用户webfix帮助我得到这个:
$ mars = array('火星有多大?','火星有多少个卫星?有?,火星有多远?,火星上的最高点是什么?);
$ jupiter = array(木星有多大?,木星有多少个卫星?,木星有多远?,木星的最高点是什么?
$ earth = array(地球有多大?,地球有多少个卫星?,地球有多远?,地球上的最高点是什么?
$ sun = array('太阳有多大?','太阳有多少个卫星','太阳有多远?''太阳的最高点是什么? );
$ all = array($ mars,$ jupiter,$ earth,$ sun);
函数createList($ a)
{
echo< ul>;
foreach($ a as $ array)
{
$ questions = count($ array);
$ idquestion = rand(0,$ questions-1);
回显< li> 。 $ array [$ idquestion]。 < /锂> 中;
}
回显< / ul>;
}
createList($ all);
现在我想添加一个问题顺序的随机化以及最多三个(或任何数量待改变)的问题吐出。
它目前将选择每个问题之一($ mars,$ jupiter,$ earth,$ sun)和然后按照这个顺序把它列成一个列表。我想命令是随机的,只有三个被选中。
我尝试使用shuffle($ all),但是这并不起作用也许我们可以使用for($ i = 1; $ i <4; $ i ++)来使它在选择三之后停止。谢谢。
shuffle($ all)
对我来说),我不知道为什么不适合你。要选三个,做:
$ mars = array('火星有多大?','火星有多少个月亮? ?,火星有多远?,火星上的最高点是什么?);
$ jupiter = array(木星有多大?,木星有多少个卫星?,木星有多远?,木星的最高点是什么?
$ earth = array(地球有多大?,地球有多少个卫星?,地球有多远?,地球上的最高点是什么?
$ sun = array('太阳有多大?','太阳有多少个卫星','太阳有多远?''太阳的最高点是什么? );
$ all = array($ mars,$ jupiter,$ earth,$ sun);
shuffle($ all);
函数createList($ a)
{
echo< ul>;
$ count = 1;
foreach($ a as $ array)
{
$ questions = count($ array);
$ idquestion = rand(0,$ questions-1);
回显< li> 。 $ array [$ idquestion]。 < /锂> 中;
if($ count ++> = 3){
break;
}
}
echo< / ul>;
}
createList($ all);
break
提前终止循环。 p>
In a previous question I didn't realize that I wouldn't be able to add in the rest of the changes myself, so here is what I'm working with. User webfix helped me get this:
$mars = array ('How big is Mars?', 'How many moons does Mars have?', 'How far away is Mars?', 'What is the highest point on Mars?');
$jupiter = array ('How big is Jupiter?', 'How many moons does Jupiter have?', 'How far away is Jupiter?', 'What is the highest point on Jupiter?');
$earth = array ('How big is Earth?', 'How many moons does Earth have?', 'How far away is Earth?', 'What is the highest point on Earth?');
$sun = array ('How big is the Sun?', 'How many moons does the Sun have?', 'How far away is the Sun?', 'What is the highest point on the Sun?');
$all = array($mars, $jupiter, $earth, $sun);
function createList($a)
{
echo "<ul>";
foreach ($a as $array)
{
$questions = count($array);
$idquestion = rand(0, $questions-1);
echo "<li>" . $array[$idquestion] . "</li>";
}
echo "</ul>";
}
createList($all);
I now want to add to this a randomization of the order of questions as well as a maximum of three (or any number to be changed later) questions spit out.
It currently will choose one of each question ($mars, $jupiter, $earth, $sun) and then make it into a list in that order. I want the order to be randomized and for only three of them to be chosen.
I tried to use "shuffle ($all)", but that didn't work and maybe we could use something like the "for ($i = 1; $i < 4; $i++)" to make it stop after picking three? Thanks.
shuffle($all)
should work (it works for me), I don't know why it isn't for you. To pick three, do:
$mars = array ('How big is Mars?', 'How many moons does Mars have?', 'How far away is Mars?', 'What is the highest point on Mars?');
$jupiter = array ('How big is Jupiter?', 'How many moons does Jupiter have?', 'How far away is Jupiter?', 'What is the highest point on Jupiter?');
$earth = array ('How big is Earth?', 'How many moons does Earth have?', 'How far away is Earth?', 'What is the highest point on Earth?');
$sun = array ('How big is the Sun?', 'How many moons does the Sun have?', 'How far away is the Sun?', 'What is the highest point on the Sun?');
$all = array($mars, $jupiter, $earth, $sun);
shuffle($all);
function createList($a)
{
echo "<ul>";
$count = 1;
foreach ($a as $array)
{
$questions = count($array);
$idquestion = rand(0, $questions-1);
echo "<li>" . $array[$idquestion] . "</li>";
if ($count++ >= 3) {
break;
}
}
echo "</ul>";
}
createList($all);
break
terminates a loop early.
这篇关于如何随机地从数组中取出例子,将它们洗牌并将它们放入一个新的数组中,以格式化为一个列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!