HTML多维数组

时间:2017-04-12 21:07:16

标签: php arrays

在下面的代码中,仅显示最后两个表单中的值。我能做错什么?

<form action="" method="post">

<input name="fields[edu][name][]" /> 
<input name="fields[edu][age][]"/> 
<br/><br/>

<input name="fields[edu][name][]" /> 
<input name="fields[edu][age][]"/> 

<br/><br/>
<input name="fields[edu][name][]" /> 
<input name="fields[edu][age][]"/> 

<br/><br/>
<input type="submit" name="submit" value="Submit"/>

if(isset($_POST['submit'])){
    print_r($_POST['fields']);

    foreach($_POST['fields'] as $field){
        echo '<br/>';
        echo 'Hello your name is : '.$field['name'];
        echo '<br/>';
        echo 'Hello your age is : '.$field['age'];
    }
}

更新:回显数组值的最佳方法是什么?我已经尝试了几种无效的方法

1 个答案:

答案 0 :(得分:0)

<form action="" method="post">

<input name="fields[edu0][name][]" /> 
<input name="fields[edu0][age][]"/> 
<br/><br/>

<input name="fields[edu1][name][]" /> 
<input name="fields[edu1][age][]"/> 

<br/><br/>
<input name="fields[edu2][name][]" /> 
<input name="fields[edu2][age][]"/> 

<br/><br/>
<input type="submit" name="submit" value="Submit"/>