Search code examples
phparrayssub-array

PHP define array with sub arrays as an index


I am using a PHP file to define some data to be used in another app.

I thought this was a valid way to declare my arrays.. with sub-arrays as indexes.

But apparently not..

//question 1
$quiz['question'][0] = "xxx";
$quiz['question'][0]['answer'][0] = "xxx";
$quiz['question'][0]['answer'][1] = "xxx";
$quiz['question'][0]['answer'][2] = "xxx";
$quiz['question'][0]['answer'][3] = "xxx";
$quiz['question'][0]['answer'][4] = "xxx";  

How do I correctly define these are easily/legible arrays?

I am currently getting this error/warning:

Warning: Illegal string offset 'answer'


Solution

  • You are explicitly saying in the first line that $quiz['question'][0] is a string, and afterwards you try to act as if its an array, thats why you are getting this error