Php 按类型遍历set$\u POST变量?

Php 按类型遍历set$\u POST变量?,php,Php,我有如下用户输入: <form action="special.php" method="post"> <input name="first1"> <input name="last1"> <input name="age1"> <input name="first2"> <input name="last2"> <input name="age2"> <input name="fi

我有如下用户输入:

<form action="special.php" method="post">
    <input name="first1"> <input name="last1"> <input name="age1">
    <input name="first2"> <input name="last2"> <input name="age2">
    <input name="first3"> <input name="last3"> <input name="age3">
    <input name="first4"> <input name="last4"> <input name="age4">
    <input name="first5"> <input name="last5"> <input name="age5">
    <input name="first6"> <input name="last6"> <input name="age6">
    ...

    N
</form>

...
N
表单中的用户输入量由用户确定;也就是说,用户可以在上面的代码中添加5、10、20行额外的代码,根据需要创建新的输入元素(按照上面的模式)

我的问题是,一旦表单被提交,迭代和打印所有SET POST变量的简单方法是什么

比如:

for($i=0; $i < $numPostVars; $i++){
   if(isset($_POST['first".$i."'])){
       //echo all first names post variables that are set
    }
}

// do the same from last names & age in separate loops
($i=0;$i<$numPostVars;$i++)的
{
如果(isset($_POST['first.$i.'])){
//回显已设置的所有名字post变量
}
}
//在单独的循环中对姓氏和年龄执行相同的操作

我认为诀窍在于将变量命名稍微不同,并利用PHP的特性,将它们解压为数组。只需使用以下语法:
first[1]
。然后在PHP中,$\u POST['first']['1']就是您可以找到它的地方。然后,您可以使用

foreach($_POST['first'] as $first_input) {
  // ... 
}
还请记住,如果用户提交时它是空的,则浏览器会停止

以下是HTML中输入的内容:

<input name="first[1]"> <input name="last[1]"> <input name="age[1]">

如用户@ DaveRandom所指出的,还考虑一个更层次的结构(如从数据库中的“行”):


我认为诀窍在于将变量命名稍微不同,并利用PHP的特性,将它们解压为数组。只需使用以下语法:
first[1]
。然后在PHP中,$\u POST['first']['1']就是您可以找到它的地方。然后,您可以使用

foreach($_POST['first'] as $first_input) {
  // ... 
}
还请记住,如果用户提交时它是空的,则浏览器会停止

以下是HTML中输入的内容:

<input name="first[1]"> <input name="last[1]"> <input name="age[1]">

如用户@ DaveRandom所指出的,还考虑一个更层次的结构(如从数据库中的“行”):


输入可以被视为数组,其语法与PHP中使用的语法非常相似:

<input name="name[1]" value="value 1">
<input name="name[2]" value="value 2">
这个原理可以扩展到包含多维数组和关联数组。因此,如果您将输入命名为:

array(
  1 => "value 1",
  2 => "value 2"
);
<input name="rows[1][first]"> <input name="rows[1][last]"> <input name="rows[1][age]">
<input name="rows[2][first]"> <input name="rows[2][last]"> <input name="rows[2][age]">
有几件事需要注意:

  • 与PHP不同,HTML中的关联数组键不需要引号,使用它们将产生您可能无法预期的结果。它会起作用,但不是以你可能认为的方式。不过,您仍然需要在PHP中使用引号
  • 据我所知,这种语法不是W3C标准。然而,PHP总是按预期处理它

输入可以作为数组处理,其语法与PHP中使用的语法非常相似:

<input name="name[1]" value="value 1">
<input name="name[2]" value="value 2">
这个原理可以扩展到包含多维数组和关联数组。因此,如果您将输入命名为:

array(
  1 => "value 1",
  2 => "value 2"
);
<input name="rows[1][first]"> <input name="rows[1][last]"> <input name="rows[1][age]">
<input name="rows[2][first]"> <input name="rows[2][last]"> <input name="rows[2][age]">
有几件事需要注意:

  • 与PHP不同,HTML中的关联数组键不需要引号,使用它们将产生您可能无法预期的结果。它会起作用,但不是以你可能认为的方式。不过,您仍然需要在PHP中使用引号
  • 据我所知,这种语法不是W3C标准。然而,PHP总是按预期处理它

您的示例有什么问题?你有错误吗?在我看来,你应该使用数组。
first[1]
first[2]
等,或者(可能更好)
行[1][first]
行[1][last]
行[1][age]
行[2][first]
。。。等等。如果你用这种语法命名你的输入,数据将已经用很好的、易于使用的数组来构造,你可以在它们上面
foreach()
——比如
foreach($\u POST['rows']as$row){/*stuff with$row['first']etc*/}
@DaveRandom,这很好,请回答,这样我就可以接受你的答案作为解决方案了。你的方法非常有效!:)非常感谢。你的例子怎么了?你有错误吗?在我看来,你应该使用数组。
first[1]
first[2]
等,或者(可能更好)
行[1][first]
行[1][last]
行[1][age]
行[2][first]
。。。等等。如果你用这种语法命名你的输入,数据将已经用很好的、易于使用的数组来构造,你可以在它们上面
foreach()
——比如
foreach($\u POST['rows']as$row){/*stuff with$row['first']etc*/}
@DaveRandom,这很好,请回答,这样我就可以接受你的答案作为解决方案了。你的方法非常有效!:)非常感谢。您甚至可以简化为输入名称中的第一个[],它将根据需要创建索引。@mjayt查看我对答案的评论。。。当您需要多个字段一致地组合在一起时,这是不安全的。这里需要多维关联数据库结果样式数组,请参阅我对该问题的评论。老实说,现在太晚了,我也太累了,无法构建一个解释得很好的答案。如果你能把它写成文字/代码,我很乐意为它投票:-)我被OP说服了,但无论如何享受这个美味的+1:-)你甚至可以简化为输入名称中的第一个[],它会根据需要创建索引。@mjay看不到我对答案的评论。。。当您需要多个字段一致地组合在一起时,这是不安全的。这里需要多维关联数据库结果样式数组,请参阅我对该问题的评论。老实说,现在太晚了,我也太累了,无法构建一个解释得很好的答案。如果你能把它用文字/代码表达出来,我很乐意投票支持:-)我被OP说服了,但无论如何,我都喜欢这个美味的+1:-)