H
H
hollanditkzn2018-02-01 14:16:31
Yii
hollanditkzn, 2018-02-01 14:16:31

How to make a multidimensional array with selections?

I need to make tests and an example in order to display everything, I figured that it would be better to display an entire array with a question and answers. Only with objects I understand how to do it, but with an array, not so much. Or it is not necessary to use an array for these purposes?
Here is my implementation

public function actionTesting($id)
    {
        $testArr = [];
        $model = Questions::find()->where(['id_theme' => $id])->all();
        foreach ($model as $key => $value){
            $testArr[] = ['id' => $value->id, 'text' => $value->name];
            $answear = Answers::find()->where(['id_question' => $value->id])->all();

            //for ($i = 0; $i<count($answear); $i++){
            //    $testArr[$i] = array_push($testArr, $answear->name);
            //}
        }

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

I get this array
array (size=3)
  0 => 
    array (size=2)
      'id' => int 1
      'text' => string 'Вопрос нулевой' (length=79)
  1 => 
    array (size=2)
      'id' => int 2
      'text' => string 'Вопрос первый?' (length=64)
  2 => 
    array (size=2)
      'id' => int 4
      'text' => string 'Вопрос 2' (length=134)

You need something like this structure
[key => [
'id' => id
'text' => text
'answear' => ['text' => text, 'answear' => answear]
]]

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question