所以下面我有一个数组,我需要从中获取数据,我需要在foreach循环中输出它:
问题:如何泡茶
回答: - 茶包 - 茶包水牛奶糖
问题:我的名字是什么
回答: - 列维 - 标记
数组输出:
array(2) {
[260]=>
object(Question)#16 (3) {
["answers"]=>
array(2) {
[0]=>
object(Answer)#18 (5) {
["id"]=>
string(3) "144"
["answer"]=>
string(4) "levi"
["questionId"]=>
string(3) "260"
["correct"]=>
string(1) "0"
["quizId"]=>
string(2) "33"
}
[1]=>
object(Answer)#19 (5) {
["id"]=>
string(3) "143"
["answer"]=>
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) {
["answers"]=>
array(2) {
[0]=>
object(Answer)#20 (5) {
["id"]=>
string(3) "142"
["answer"]=>
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"
["answer"]=>
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 />';
}
}
答案 0 :(得分:2)
您的顶级数组$ 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 />';
}