How to organize your tests in php?

I brought the data from the database. I have built 1 so the topic is let's say 3 questions(can be more than 10-15 questions) and each question 3 answer(there may be more answers).
And they are all related and to bring them out, I have a question how to do it. If not sculpt your array. My example implementation
Controller
public function actionTesting($id)
{
 $answear = Answers::find()->where(['id_thema' => $id])->all();

 return $this->render('test', [
 'model' => $model,
 'answear' => $answear,
]);
 }

In the view
<?php
$this--->title = $answear[0]->thema->name;
?>

<div class="test-testing">

 <?php foreach ($answear as $value){
 echo $value--->question->name.'<br>';
/** Here it is not clear how to display*/
 echo $value->text.'<br>';
 } ?>

</div>

Otherwise I get
The first question
a day or two
The first question
more than 5 days
The first question
3-5 days
The second question
Yes
The second question
no
The second question
not all the time, constantly Changing
The third question
Yes
The third question
no
The third question
I do not remember

I need
The first question
- one or two days
- more than 5 days
- 3-5 days

How is it done?
June 8th 19 at 17:04
1 answer
June 8th 19 at 17:06
Solution
Overdosing should not answers but questions, and a link to pull the answers to them
$questions = Question::find()
 ->with('answers') //will ship in one request all the answers to the selected questions
->all();
foreach ($questions as $question){
 echo $question->name . '<br>';
 foreach ($question->answers as $answer){
 echo $answer->text . '<br>';
}
}

But then the question in General of the feasibility to store questions and answers in separate tables, I'd keep it in one. Answers in the form of json and another cell with the key for correct answer.
id | question | answers | right
In afterFind given json into a regular php array, using the JsonHelper and yuzaem.
foreach (Question::find()->all() as $question){
 echo $question->name . '<br>';
 foreach ($question->answers as $key=>$answer){
 echo $answer . '<br>';
}
}
To use it as I understand right?
https://nix-tips.ru/yii2-api-guides/guide-ru-db-ac...
Or I not so think? - Waylon_Kub commented on June 8th 19 at 17:09
Of course (only the staging table You do not have a simple hasMany relationship). But I added the answer, I'm not sure that it makes sense to store in different tables. - Henderson_Beat commented on June 8th 19 at 17:12
I just relied on the fact that can be 3 questions, and somewhere 4 or more questions. And the second point where not only one correct answer and multiple correct answers. So I did split them.
But it seemed to me that it would be more correct - Waylon_Kub commented on June 8th 19 at 17:15
I just relied on the fact that can be 3 questions, and somewhere 4 or more questions

what? At least 20.

And the second point where not only one correct answer and multiple correct answers

then in the cell with the correct answer not a number, but an array of numbers in the form of a json - Henderson_Beat commented on June 8th 19 at 17:18
That is offered in the questions and answers to store json right? or not? - Waylon_Kub commented on June 8th 19 at 17:21
No , the questions are kept the same. I gave the structure:
id (integer) | question (string) | answers (string json) | right (string json) | something_else - Henderson_Beat commented on June 8th 19 at 17:24
why in beforeFind()? If I understand correctly the logic of events, it is called when there is no data that needs to be changed, no? - daron commented on June 8th 19 at 17:27
You are absolutely right, I was wrong, of course afterFind, corrected, thanks.
In beforeSave make json array in afterFind Vice versa. - Henderson_Beat commented on June 8th 19 at 17:30
Slightly maybe I'm not caught up, a question how is the initial data conversion?
Stored in database the following data
{
"answear": ["Answer 1, Answer 2, Answer 3, Answer 4"]
}

Or is there another format it is necessary to throw? - Waylon_Kub commented on June 8th 19 at 17:33
{"Answer 1, Answer 2, Answer 3, Answer 4"} - Henderson_Beat commented on June 8th 19 at 17:36

Find more questions by tags Yii