且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何随机地从数组中取出例子,将它们洗牌并将它们放入一个新的数组中,以格式化为一个列表

更新时间:2023-11-21 23:33:40

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]。 &LT; /锂> 中;
if($ count ++> = 3){
break;
}
}
echo< / ul>;
}

createList($ all);

break 提前终止循环。 p>

DEMO


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.

DEMO