数组输出问题及其问题


Array output questions and its asnwers

所以下面我有一个数组,我需要从中获取数据,我需要在foreach循环中输出它:

问题:如何泡茶

答案:- 茶包- 茶包水牛奶糖

问题:我叫什么名字

答案:-利瓦伊-马克

数组输出:

array(2) {
  [260]=>
  object(Question)#16 (3) {
    ["answerswers"]=>
    array(2) {
      [0]=>
      object(Answer)#18 (5) {
        ["id"]=>
        string(3) "144"
        ["answerswer"]=>
        string(4) "levi"
        ["questionId"]=>
        string(3) "260"
        ["correct"]=>
        string(1) "0"
        ["quizId"]=>
        string(2) "33"
      }
      [1]=>
      object(Answer)#19 (5) {
        ["id"]=>
        string(3) "143"
        ["answerswer"]=>
        string(4) "mark"
        ["questionId"]=>
        string(3) "260"
        ["correct"]=>
        string(1) "0"
        ["quizId"]=>
        string(2) "33"
      }
    }
    ["id"]=>
    string(3) "260"
    ["question"]=>
    string(15) "What is my name"
  }
  [259]=>
  object(Question)#17 (3) {
    ["answerswers"]=>
    array(2) {
      [0]=>
      object(Answer)#20 (5) {
        ["id"]=>
        string(3) "142"
        ["answerswer"]=>
        string(7) "tea bag"
        ["questionId"]=>
        string(3) "259"
        ["correct"]=>
        string(1) "0"
        ["quizId"]=>
        string(2) "33"
      }
      [1]=>
      object(Answer)#21 (5) {
        ["id"]=>
        string(3) "141"
        ["answerswer"]=>
        string(24) "tea bag water milk sugar"
        ["questionId"]=>
        string(3) "259"
        ["correct"]=>
        string(1) "0"
        ["quizId"]=>
        string(2) "33"
      }
    }
    ["id"]=>
    string(3) "259"
    ["question"]=>
    string(15) "How to make tea"
  }
}

将不胜感激的帮助

我的开始,但根本不起作用只是一个想法:

$questions =  Quiz::OutputQuestions($id);
foreach($questions as $question) {
    echo 'Question: ' . $question['id']['question'];
    echo 'Answers: <br />';
    foreach($answers as $answer) {
          echo '- Answer <br />';
    }
}
似乎

您的顶级数组$questions中有对象而不是数组。所以你可以试试这段代码:

$questions =  Quiz::OutputQuestions($id);
foreach($questions as $question) {
    echo 'Question: ' . $question->question;
    echo 'Answers: <br />';
    foreach($question->answers as $answer) {
          echo '- ' . $answer->answer;
    }
    echo '<br />';
}