Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

Greggus's avatar

Can't store data from multiple choice questions. (Illegal string offset 'answer')

Im having some difficulty trying to store the results from my multiple choice questions. So I have have 2 tables [questions] and [answers]. All the questions are stored in the question table and when I want to display then I have this

`{{!!Form::open(...)!!}}
    @foreach ($questions as $question)

    <div>{{$question->question}}</div>
         <div class="form-group">
             {!! Form::label('answer', 'Score') !!}
            {{ Form::selectRange(' answer[]', 1, 10) }}
            {{ Form::hidden('question_id[]', $question->id) }}
            {{ Form::hidden('user_id[]', $userId) }}
    </div>
    @endforeach
{{!!Form::submit(...)!!}}
{{!!Form::close()!!}}`

Ok so im also passing the question_id and the user_id which is the same for all the questions. My controller looks like this.

`public function store(Request $request)
{
        $arr_answers = $request->all();

        foreach ($arr_answers as $row) {

        QuestionResults::create([
                'answer' => $row['answer'],
                'question_id' => $row['question_id'],
             'user_id' => $row['user_id'],
          ]);
     }

 return "working";`

$arr_answers returns 3 arrays, the answer, the question_id and the user_id, all with the correct information, so my thought process is the foreach loop should return an int for each value but unfortunately its not playing ball and returning "Illegal string offset".

Please could someone point me in the direction of a solution or a better method to doing this.

0 likes
7 replies
bobbybouwmann's avatar

What does $arr_answers return exactly? Can you post that here?

Greggus's avatar

The output for $arr_answers is :

{"_token":"inpbAEmqWNY7pCrmkonmVrgG3X3wyn90hG25xaNJ","answer":["1","1","1","1","1","1","1","1","1","1","1","1"],"question_id":["1","2","3","4","5","6","7","8","9","10","11","12"],"user_id":["1","1","1","1","1","1","1","1","1","1","1","1"]}

Greggus's avatar
Greggus
OP
Best Answer
Level 1

I found a solution,

 foreach ($arr_answers['answer'] as $key => $row) {

  QuestionResults::create([
    'answer'      => $row,
    'question_id' => $arr_answers['question_id'][$key],
    'user_id'     => $arr_answers['user_id'][$key],
]);

}

Greggus's avatar

Im still not sure why this works, if you don't mind, would you explaining a little or point me in the direction to read up on it, I'd appreciate it massively.

bobbybouwmann's avatar

Right now when you submit the data from your application you will get an array from each key that you post. But they will always have the same array key. So in an array you have a key value pair. Most of the time that's an integer with some value

[
    0 => value
    1 => value
    2 => value
]

So when you loop over $answers['answer'] you're basically looping over one of the posted values. Now since every posted key has the same amount of key value pairs you will always make this work.

In your foreach you say $answers['answer'] as $key => $row. Here is $key the number from the array (0, 1, 2) and $row the actual posted in the answer input from your application.

So now you have the key of the array you can simply ask the other arrays, give me the value that matches this key ($arr_answers['question_id'][$key]).

Let me know if this is better for you. Currently in the train which makes it harder to type!

1 like
Greggus's avatar

Thank you for taking the time to explaining this to me, I understand it much better now. The $key was throwing me off but I get it now, thanks again.

Please or to participate in this conversation.