我写了一个函数,我必须用三个不同的答案返回同一个问题。我设法提出了一个问题(我应该这样做,因为这三个问题都是一样的),但我很难弄清楚如何得出所有答案。转储$questions返回所有三个,但我的 foreach 只返回第一个。$questions = $this->getQuestionRepository()->findBy([ 'user' => $user, ]); $result = []; foreach ($questions as $question) { $result['question'] = $question->getQuestion(); foreach ($questions as $answer) { $result['answers'] = [$answer->getAnswer()]; } } return $result;结果:"data": { "question": "What is your name?", "answers": [ "First answer" ]},
1 回答
慕慕森
TA贡献1856条经验 获得超17个赞
尝试这个:
$questions = $this->getQuestionRepository()->findBy([
'user' => $user,
]);
$result = [];
foreach ($questions as $question) {
$result['question'] = $question->getQuestion();
foreach ($questions as $answer) {
$result['answers'][] = $answer->getAnswer();
}
}
return $result;
- 1 回答
- 0 关注
- 101 浏览
添加回答
举报
0/150
提交
取消