我在下面有一个关联数组:
$questions = array();
while ($selectedstudentanswerstmt->fetch()) {
$questions[$detailsStudentId][$detailsQuestionId] = array(
'questionno'=>$detailsQuestionNo,
'content'=>$detailsQuestionContent
);
}
现在我想在每个循环中显示信息,但我的问题是应该调用foreach循环,因为我认为下面的内容不正确,因为它一直说questionno
和content
未定义在循环中:
var_dump($questions);
foreach ($questions as $questionId => $question) {
//LINE 571
echo '<p><strong>Question:</strong> ' .htmlspecialchars($question['questionno']). ': ' .htmlspecialchars($question['content']) . '</p>' . PHP_EOL;
}
Notice: Undefined index: questionno in ... on line 571
Notice: Undefined index: content in ... on line 571
答案 0 :(得分:0)
$questions[$detailsStudentId][$detailsQuestionId] = array(...)
在关联索引开始之前,你有两个维度。所以你需要在其中嵌套另一个foreach循环:
foreach ($questions as $stundentId => $student)
{
foreach ($student as $questionId => $question)
{
// ...
}
}