在隐藏的HTML输入字段中使用方括号

时间:2022-04-03 08:52:44

I am analyzing someone else's PHP code and I've noticed that the input HTML has many hidden input fields with names that end with '[]', for instance:

我正在分析其他人的PHP代码,我注意到输入HTML有许多隐藏的输入字段,其名称以'[]'结尾,例如:

<input type="hidden" name="ORDER_VALUE[]" value="34" />
<input type="hidden" name="ORDER_VALUE[]" value="17" />

The PHP page that processes this input acquires each value like this:

处理此输入的PHP页面获取每个值,如下所示:

foreach ($_REQUEST["ORDER_VALUE"] as $order_value) {
    /...
}

What is the '[]' used for? Specifying that there would be multiple input fields with the same name?

什么是'[]'用于?指定会有多个具有相同名称的输入字段?

4 个解决方案

#1


Yes. Basically PHP will know to stick all of those values with the same name into an array.

是。基本上PHP会知道将所有具有相同名称的值粘贴到数组中。

This applies to all input fields, by the way, not just hidden ones.

顺便说一下,这适用于所有输入字段,而不仅仅是隐藏字段。

#2


It passes data as an array to PHP. When you have HTML forms with the same name it will append into comma lists like checkbox lists. Here PHP has processing to convert that to a PHP array based on the [] like so:

它将数据作为数组传递给PHP。当您具有相同名称的HTML表单时,它将附加到逗号列表中,如复选框列表。这里有PHP处理将其转换为基于[]的PHP数组,如下所示:

To get your result sent as an array to your PHP script you name the , or elements like this:

要将结果作为数组发送到PHP脚本,请将这些或类似的元素命名为:

<input name="MyArray[]" />
<input name="MyArray[]" />
<input name="MyArray[]" />
<input name="MyArray[]" />

Notice the square brackets after the variable name, that's what makes it an array. You can group the elements into different arrays by assigning the same name to different elements:

注意变量名后的方括号,这就是使它成为数组的原因。您可以通过为不同的元素指定相同的名称将元素分组到不同的数组中:

<input name="MyArray[]" />
<input name="MyArray[]" />
<input name="MyOtherArray[]" />
<input name="MyOtherArray[]" />

This produces two arrays, MyArray and MyOtherArray, that gets sent to the PHP script. It's also possible to assign specific keys to your arrays:

这会产生两个数组,MyArray和MyOtherArray,它们被发送到PHP脚本。也可以为阵列分配特定的键:

<input name="AnotherArray[]" />
<input name="AnotherArray[]" />
<input name="AnotherArray[email]" />
<input name="AnotherArray[phone]" />

http://us2.php.net/manual/en/faq.html.php

#3


See How do I create arrays in a HTML <form>? in the PHP FAQ.

请参阅如何在HTML

中创建数组?在PHP FAQ中。

#4


Most form processing libraries expect the author to state if they want to treat a piece of data as a string or an array of strings.

大多数表单处理库都希望作者说明他们是想将一段数据视为字符串还是字符串数组。

The authors of PHP decided to be inconsistent with the rest of the world, and require that the HTML be constructed specially.

PHP的作者决定与世界其他地方不一致,并要求专门构建HTML。

Putting square brackets at the end of the name tells PHP to treat it as an array of data.

将方括号放在名称的末尾会告诉PHP将其视为数据数组。

#1


Yes. Basically PHP will know to stick all of those values with the same name into an array.

是。基本上PHP会知道将所有具有相同名称的值粘贴到数组中。

This applies to all input fields, by the way, not just hidden ones.

顺便说一下,这适用于所有输入字段,而不仅仅是隐藏字段。

#2


It passes data as an array to PHP. When you have HTML forms with the same name it will append into comma lists like checkbox lists. Here PHP has processing to convert that to a PHP array based on the [] like so:

它将数据作为数组传递给PHP。当您具有相同名称的HTML表单时,它将附加到逗号列表中,如复选框列表。这里有PHP处理将其转换为基于[]的PHP数组,如下所示:

To get your result sent as an array to your PHP script you name the , or elements like this:

要将结果作为数组发送到PHP脚本,请将这些或类似的元素命名为:

<input name="MyArray[]" />
<input name="MyArray[]" />
<input name="MyArray[]" />
<input name="MyArray[]" />

Notice the square brackets after the variable name, that's what makes it an array. You can group the elements into different arrays by assigning the same name to different elements:

注意变量名后的方括号,这就是使它成为数组的原因。您可以通过为不同的元素指定相同的名称将元素分组到不同的数组中:

<input name="MyArray[]" />
<input name="MyArray[]" />
<input name="MyOtherArray[]" />
<input name="MyOtherArray[]" />

This produces two arrays, MyArray and MyOtherArray, that gets sent to the PHP script. It's also possible to assign specific keys to your arrays:

这会产生两个数组,MyArray和MyOtherArray,它们被发送到PHP脚本。也可以为阵列分配特定的键:

<input name="AnotherArray[]" />
<input name="AnotherArray[]" />
<input name="AnotherArray[email]" />
<input name="AnotherArray[phone]" />

http://us2.php.net/manual/en/faq.html.php

#3


See How do I create arrays in a HTML <form>? in the PHP FAQ.

请参阅如何在HTML

中创建数组?在PHP FAQ中。

#4


Most form processing libraries expect the author to state if they want to treat a piece of data as a string or an array of strings.

大多数表单处理库都希望作者说明他们是想将一段数据视为字符串还是字符串数组。

The authors of PHP decided to be inconsistent with the rest of the world, and require that the HTML be constructed specially.

PHP的作者决定与世界其他地方不一致,并要求专门构建HTML。

Putting square brackets at the end of the name tells PHP to treat it as an array of data.

将方括号放在名称的末尾会告诉PHP将其视为数据数组。