Php 如何将$\u POST变量分组到正确的数组中

Php 如何将$\u POST变量分组到正确的数组中,php,arrays,foreach,Php,Arrays,Foreach,我正在用foreach循环生成下面的html表单。在$people中有4个$person数组,因此下面的输入集重复了4次 <form action="handler.php" method="post"> <?php foreach($people as $person) { ?> <input type="text" name="first_name"> <input type="text" name="middle_name">

我正在用foreach循环生成下面的html表单。在$people中有4个$person数组,因此下面的输入集重复了4次

<form action="handler.php" method="post">
  <?php foreach($people as $person) { ?>
  <input type="text" name="first_name">
  <input type="text" name="middle_name">
  <input type="text" name="last_name">
  <input type="hidden" name="<?php echo $person['id'];?>
  <?php } ?>
</form>
等等


但当我返回时,我不知道如何将每个人放入一个单独的数组中。我认为它可能需要一些关于“name”属性的技巧,但是还没能让它工作

这可能就是您想要的:

<form action="handler.php" method="post">
  <?php foreach($people as $person) {
    echo sprintf('<input type="text" name="first_name[%s]">', $person['id']);
    echo sprintf('<input type="text" name="middle_name[%s]">', $person['id']);
    echo sprintf('<input type="text" name="last_name[%s]">', $person['id']);
  } ?>
</form>


在html标记中使用数组表示法:
这将不输出任何内容。不,将不输出任何内容。您介意分享这个问题吗?还是说谜语对您更重要?;-)我以为朝正确的方向轻推会帮助你看到它。。。您需要
echo sprintf(…)
printf(…)
<form action="handler.php" method="post">
  <?php foreach($people as $person) {
    echo sprintf('<input type="text" name="first_name[%s]">', $person['id']);
    echo sprintf('<input type="text" name="middle_name[%s]">', $person['id']);
    echo sprintf('<input type="text" name="last_name[%s]">', $person['id']);
  } ?>
</form>
<?php
    foreach ($people as $person) {
        printf('<input type="text" name="person[%d][first_name]">',  $person['id']);
        printf('<input type="text" name="person[%d][middle_name]">', $person['id']);
        printf('<input type="text" name="person[%d][last_name]">',   $person['id']);
    }
?>
var_dump($_POST['person']);