没有。只有最后一个输入元素可用。
如果您想要多个具有相同名称的输入,请使用name="foo[]" 作为输入名称属性。然后$_POST 将包含一个 foo 数组,其中包含来自输入元素的所有值。
<form method="post">
<input name="a[]" value="foo"/>
<input name="a[]" value="bar"/>
<input name="a[]" value="baz"/>
<input type="submit" />
</form>
请参阅HTML reference at Sitepoint。
如果你不使用[],$_POST 将只包含最后一个值的原因是因为 PHP 基本上只会爆炸并遍历原始查询字符串以填充 $_POST。当它遇到一个已经存在的名称/值对时,它会覆盖之前的。
但是,您仍然可以像这样访问原始查询字符串:
$rawQueryString = file_get_contents('php://input'))
假设你有这样的表格:
<form method="post">
<input type="hidden" name="a" value="foo"/>
<input type="hidden" name="a" value="bar"/>
<input type="hidden" name="a" value="baz"/>
<input type="submit" />
</form>
然后 $rawQueryString 将包含 a=foo&a=bar&a=baz。
然后您可以使用自己的逻辑将其解析为数组。一种天真的方法是
$post = array();
foreach (explode('&', file_get_contents('php://input')) as $keyValuePair) {
list($key, $value) = explode('=', $keyValuePair);
$post[$key][] = $value;
}
然后会为查询字符串中的每个名称提供一个数组数组。