bdereta
bdereta

Reputation: 913

control array output with input fields

I have sets of input fields:

<input type="text" name="data[question_id][]" value="6" />
<input type="text" name="data[position][]" value="50"  />
<input type="text" name="data[answer][]" value="London" />

<input type="text" name="data[question_id][]" value="6" />
<input type="text" name="data[position][]" value="60"  />
<input type="text" name="data[answer][]" value="New York" />

Here's my output:

array (
'question_id' => 
    array (
        0 => '6',
        1 => '6',
    ),
'position' => 
    array (
        0 => '50',
        1 => '60',      
    ),
'answer' => 
    array (
        0 => 'London',
        1 => 'New York',
    ),
)

However, I need the array to be in the following format:

array (
0 =>
    array (
        'question_id' => '6',
        'position' => '50',
        'answer' => 'London',
    ),
1 =>
    array (
        'question_id' => '7',
        'position' => '60',
        'answer' => 'New York',
    ),
)

I tried placing the brackets after data (data[][question_id]), but the array becomes even more convoluted. Thank you for your time!

Upvotes: 0

Views: 601

Answers (1)

Rick Viscomi
Rick Viscomi

Reputation: 8852

<input type="text" name="data[0][question_id]" value="6" />
<input type="text" name="data[0][position]" value="50"  />
<input type="text" name="data[0][answer]" value="London" />

<input type="text" name="data[1][question_id]" value="6" />
<input type="text" name="data[1][position]" value="60"  />
<input type="text" name="data[1][answer]" value="New York" />

(per the comments in the original question)

Upvotes: 1

Related Questions