turning 2-D form data into a 2-D array

I am new to PHP.
I have a form program that records 2-dimensional info. It records info like this:


I would like to save it to produce an array that looks like this for display purposes. (I can get it to only print_r arrays seperately)

   1 => array(
   2=> array(

Any ideas?

Admin Edit: Added CODE tags to preserve formatting.

PHP DOES support multi-dimensional indexes


$QandA[1]['question'] = "What color is the sky?";
$QandA[1]['answer'] = "Blue";

$QandA[2]['question'] = "What is the third planet from the sun?";
$QandA[2]['answer'] = "Earth";

$QandA[3]['question'] = "What is the Best Web Scripting Language?";
$QandA[3]['answer'] = "PHP Of course!";

For your example you have 2 answers to ONE question. No problem. Just make an index of answer1 AND an index of answer2

$QandA[3]['question'] = "What is the Best Web Scripting Language?";
$QandA[3]['answer1'] = "PHP Of course!";
$QandA[3]['answer2'] = "Is there any other language than PHP?!";

You can expand it beyond 2 dimensional, but they generally get very complex very quickly at that point.

I am trying to take a list of info and from an array and then put that info into another array.
Take this info:

and produce this:


I know there is a simple function out there but I can’t seem to figure it out. Thanks.

Ok… but heres the problem…
ONCE you get to $A[2] and it’s Answers you will overwrite $A[1] answers in $B[1] and $B[2]
This is why a 2 dimensional array would be more suited. In my example above the index of 1 would be the Question number
Then the you can create the 2nd dimension index to represent the question (itself) and the answers that go with question 1 (or whatever the question number from the first index is.

To populate it (if it’s in another array) you can just use loops or foreach statements.

Sponsor our Newsletter | Privacy Policy | Terms of Service