-1

上一个问题中,我没有意识到我自己无法添加其余的更改,所以这就是我正在使用的。用户 webfix 帮助我得到了这个:

$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);

我现在想添加问题顺序的随机化以及最多三个(或稍后更改的任何数量)问题吐出。

它目前将选择每个问题中的一个($mars、$jupiter、$earth、$sun),然后按该顺序将其放入列表中。我希望顺序是随机的,并且只选择其中三个。

我尝试使用“shuffle ($all)”,但这不起作用,也许我们可以使用类似“for ($i = 1; $i < 4; $i++)”的东西让它在选择三个后停止? 谢谢。

4

1 回答 1

1

shuffle($all)应该工作(它适用于我),我不知道为什么它不适合你。要选择三个,请执行以下操作:

$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提前终止循环。

演示

于 2013-08-16T20:55:49.237 回答