合計 n00b から PHP ですが、非常に興味があります。多次元配列には、クイズのすべての質問のオプションと回答の両方を含める必要がある単一形式の php クイズの一部として、深い多次元配列を使用することに現在苦労しています。
これは、私が特に見ている配列の最初の部分です。
$quizquestions = array ("question1" => array ("type" => "checkbox",
"text" => "question text goes here",
"options" => array ([1] => "option 1 goes here",
[2] => "option 2 goes here",
[3] => "option 3 goes here",
[4] => "options 4 goes here",
[5] => "option 5 goes here",
[6] => "option 6 goes here"
), //end of options array
"answers" => array ([1] => " right answer one here",
[3] => "right answer 2",
[6] => "right answer 3 here"
), //end of answers array
"feedback" => "text that is returned after user answers"
), //end of question 1 array... question 2 begins, etc...
上記の例では、質問 1 はチェックボックスであり、正しいと見なされる 3 つの回答 (1、3、および 6) があります。ネストされた foreach ループを試して質問のオプションを取得しようとしましたが、正しい構文を取得できないようです。または、回答配列に対して を評価するために使用する必要がある関数を特定できません。使用できますかin_array
?いくつかのforeach
ステートメントを介してフィードしたもの。多次元配列で?
回答配列にあるかどうかに応じて、ユーザーが送信した回答の横にチェックまたは X を付けて、回答をリストに出力することを望んでいました。
これが私がこれまで試してきたことですが、何も返されません。何百万ものバリエーションを試しましたが、間違った方向に進んでいる可能性があります...
foreach ($quizquestions as $question => $description) {
foreach ($description as $option => $response) {
if (in_array($option, $response['question1']['answers'])) {
print "<li class='correct'>$options</li><br />\n";
$score++;
} else {
print "<li class='incorrect'>$options</li><br />\n";
} //end of if(in_array() )
} //end of description foreach
} //end of quizquestion foreach
何かご意見は?初心者を助けてください!