php HTML 输入数组

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1010941/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 00:37:47  来源:igfitidea点击:

HTML input arrays

phphtmlarraysforms

提问by gak

<input name="foo[]" ... >

I've used these before, but I'm wondering what it is called and if there is a specification for it?

我以前用过这些,但我想知道它叫什么,是否有规范?

I couldn't find it in the HTML 4.01 Specand results in various Google results only call it an "array" along with many PHP examples of processing the form data.

我在HTML 4.01 规范中找不到它,结果在各种 Google 结果中只将其称为“数组”以及许多处理表单数据的 PHP 示例。

采纳答案by The name of my cat

There are some references and pointers in the comments on this page at PHP.net:

PHP.net 上此页面的评论中有一些参考和指针:

Torsten says

托斯滕 说

"Section C.8 of the XHTML spec's compatability guidelines apply to the use of the name attribute as a fragment identifier. If you check the DTD you'll find that the 'name' attribute is still defined as CDATA for form elements."

“XHTML 规范的 C.8 部分兼容性指南适用于使用 name 属性作为片段标识符。如果您检查 DTD,您会发现 'name' 属性仍然定义为表单元素的 CDATA。”

Jetboy says

Jetboy 说

"according to this: http://www.w3.org/TR/xhtml1/#C_8the type of the name attribute has been changed in XHTML 1.0, meaning that square brackets in XHTML's name attribute are not valid.

Regardless, at the time of writing, the W3C's validator doesn't pick this up on a XHTML document."

“根据此:http: //www.w3.org/TR/xhtml1/#C_8name 属性的类型已在 XHTML 1.0 中更改,这意味着 XHTML 的 name 属性中的方括号无效。

无论如何,在撰写本文时,W3C 的验证器并未在 XHTML 文档中发现这一点。”

回答by sqram

It's just PHP, not HTML.

它只是 PHP,而不是 HTML。

It parses all HTML fields with [] into an array.

它将所有带有 [] 的 HTML 字段解析为一个数组。

So you can have

所以你可以有

<input type="checkbox" name="food[]" value="apple" />
<input type="checkbox" name="food[]" value="pear" />

and when submitted, PHP will make $_POST['food'] an array, and you can access its elements like so:

提交后,PHP 会将 $_POST['food'] 设为一个数组,您可以像这样访问它的元素:

echo $_POST['food'][0]; // would output first checkbox selected

or to see all values selected:

或查看所有选定的值:

foreach( $_POST['food'] as $value ) {
    print $value;
}

Anyhow, don't think there is a specific name for it

无论如何,不​​要认为它有一个特定的名称

回答by Paolo Bergantino

As far as I know, there isn't anything on the HTML specs because browsers aren't supposed to do anything different for these fields. They just send them as they normally do and PHP is the one that does the parsing into an array, as do other languages.

据我所知,HTML 规范中没有任何内容,因为浏览器不应该对这些字段做任何不同的事情。他们只是像往常一样发送它们,PHP 和其他语言一样,将解析为数组。

回答by Rabby shah

Follow it...

跟着它...

<form action="index.php" method="POST">
<input type="number" name="array[]" value="1">
<input type="number" name="array[]" value="2">
<input type="number" name="array[]" value="3"> <!--taking array input by input name array[]-->
<input type="number" name="array[]" value="4">
<input type="submit" name="submit">
</form>
<?php
$a=$_POST['array'];
echo "Input :" .$a[3];  // Displaying Selected array Value
foreach ($a as $v) {
    print_r($v); //print all array element.
}
?>