0

When I want to get a member of an array and assign it to a variable it returns error to me: Undefined offset: 0

$answers=Answer::where(
    [
        ['questionid', '=', $question['questionid']],
        ['uniqid','=',$uniqCode]
    ]
    )->get()
    ->toArray();
$answer=$answers[0]['answer'];

but when I print it exactly before assignment it returns the value correctly:

dd($answers[0]['answer']) ;
$answer=$answers[0]['answer'];
Lelio Faieta
  • 6,457
  • 7
  • 40
  • 74
HamHp
  • 23
  • 8

1 Answers1

0

first, check that the array actually exists, you could try something like

if(isset($answers[0])) {
      //grab value here
      $answer=$answers[0]['answer'];
  }
anthony_718
  • 424
  • 7
  • 10
  • the array exist, if it doesn't exist how is it printing the current value by dd() method!? @anthony_718 – HamHp Jun 09 '21 at 17:54