簡體   English   中英

在PHP中使用多維數組創建測驗

[英]Creating a quiz using a multidimensional array in PHP

我是PHP的新手,我需要使用多維關聯數組來創建測驗。 測驗按預期的方式打印出來,但是我遇到兩個問題,一個是我在嘗試使單選按鈕變粘時遇到了麻煩。 另一個問題是我希望鍵“ 3”是所有問題的答案,而我似乎無法找出一種方法來計算“ 3”被選中並打印出答案的次數。 在過去的7個小時中,我嘗試了許多不同的操作,但似乎並沒有按照我希望的方式工作。 你有什么提示或建議?

$quiz = array(
"What does HTML stand for?" => array(
    '1' => "Home Tool Markup Language",
    '2' => "Hyperlinks and Text Markup Language",
    '3' => "Hyper Text Markup Language",
    '4' => "Hyper Text Manipulation Language",
),
"Choose the correct HTML tag for the smallest heading:" => array(
    '2' => "<heading>",
    '1' => "<h1>",
    '4' => "<head>",
    '3' => "<h6>",
),
);
foreach($quiz as $question => $answers) {
    echo $question;
    echo "<form>";
foreach($answers as $index => $answer) {
    echo "<input type='radio' name=$option>".$answer."<br/>";
}
}
?>

您修改的代碼:

$quiz = array(
"What does HTML stand for?" => array(
    '1' => "Home Tool Markup Language",
    '2' => "Hyperlinks and Text Markup Language",
    '3' => "Hyper Text Markup Language",
    '4' => "Hyper Text Manipulation Language",
),
"Choose the correct HTML tag for the smallest heading:" => array(
    '2' => "&lt;heading&gt;",
    '1' => "&lt;h1&gt;",
    '4' => "&lt;head&gt;",
    '3' => "&lt;h6&gt;",
),
);

echo "<form>";
foreach($quiz as $question => $answers) {
    echo $question;
    foreach($answers as $index => $answer) {
        echo "<input type=\"radio\" name=\"$question\" value=\"$option\">$answer<br/>";
    }
}
echo "</form>";

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM