PHPアプリケーションに関連する3つのMySQLテーブルを使用しています。多くの質問があるテストがあり、質問には多くの答えがあります。私がやりたいのは、次のようにデータをループすることです。
foreach($tests as $test)
{
echo $test->testName;
foreach($test->questions as $question)
{
echo $question->questionText;
foreach($question->answers as $answer)
{
echo $answer->answerText;
}
}
}
私が知りたいのは、MySQLクエリとPHPコードがこの方法でループすることです。
編集MySQLはこのような配列を返すことはできません。私が言わなければならないのは、MySQL+PHPコードがどのようになるかということです。
わかりやすくするために、表はテスト、質問、および回答です。質問テーブルにはtest_id列が含まれ、回答にはquestion_idが含まれます
ありがとう!
私が取り戻そうとしている構造は、次のようなものになります。
array(
'testName' = 'Test name string',
'questions' = array(
array(
'questionId' = 1,
'questionText' = 'Question string',
'answers' = array(
array(
'answerId' = 1,
'answerText' = 'Answer string'
),
array(
'answerId' = 2,
'answerText' = 'Answer string'
)
)
)
)
);
編集
私の現在の実装は次のとおりです。私がやりたかったのは、それほど多くのクエリを実行するのではなく、データを熱心にロードすることでした。
$tests = getTests();
foreach($tests as $test){
$questions = getQuestions($test->id);
foreach($questions as $question){
$answers = getAnswers($question->id);
foreach($answers as $answer){
// do answer things
}
}
}