-3

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'

whispers
  • 962
  • 1
  • 22
  • 48

2 Answers2

1

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

RiggsFolly
  • 93,638
  • 21
  • 103
  • 149
Moses Schwartz
  • 2,857
  • 1
  • 20
  • 32
  • DOH!.. you are totally correct! it should have been: $quiz['question'][0]['question'] Thank you for staying on topic and posting my error (accepted) – whispers Aug 29 '19 at 16:33
1

How to define, well one way

$quiz['question'][0]['question'] = "How to define an array";
$quiz['question'][0]['answer'][0] = "Like this";
$quiz['question'][0]['answer'][1] = "or like that";
$quiz['question'][0]['answer'][2] = "or maybe like the other";
print_r($quiz);

RESULT

Array
(
    [question] => Array
        (
            [0] => Array
                (
                    [question] => How to define an array
                    [answer] => Array
                        (
                            [0] => Is it like this
                            [1] => Or isit like that
                            [2] => Or is it like the other
                        )

                )

        )

)
RiggsFolly
  • 93,638
  • 21
  • 103
  • 149