0

我在下面有一个关联数组:

$questions = array();

while ($selectedstudentanswerstmt->fetch()) {           
    $questions[$detailsStudentId][$detailsQuestionId] = array(
        'questionno'=>$detailsQuestionNo,
        'content'=>$detailsQuestionContent
    );    
}

现在我想在 for each 循环中显示这里的信息,但我的问题是应该调用 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 
4

1 回答 1

0
$questions[$detailsStudentId][$detailsQuestionId] = array(...)

在关联索引开始之前,您有两个维度。因此您需要在其中嵌套另一个 foreach 循环:

foreach ($questions as $stundentId => $student)
{
    foreach ($student as $questionId => $question)
    {
        // ...
    }
}
于 2013-02-08T01:21:41.557 回答