控制数组输出与输入字段

时间:2013-05-01 20:21:22

标签: php html

我有一组输入字段:

<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" />

这是我的输出:

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

但是,我需要数组采用以下格式:

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

我尝试在数据(data [] [question_id])之后放置括号,但数组变得更加复杂。谢谢你的时间!

1 个答案:

答案 0 :(得分:1)

<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" />

(根据原始问题中的评论)