包含方括号的表单输入字段名称,如字段[index] [英] Form input field names containing square brackets like field[index]

查看:143
本文介绍了包含方括号的表单输入字段名称,如字段[index]的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我见过很多处理表单输入的PHP代码,其中输入字段名称包含方括号。我明白,当PHP脚本检查$ _POST变量时,这会以某种方式导致PHP数组。



HTML示例:

 < form action ='http://zzz.com'method ='post'> 
< input name ='fruit [1]'value ='apple'/>
< input name ='fruit [2]'value ='banana'/>
< / form>

示例网址:

  http://zzz.com?fruit [1] =苹果和水果[2] =香蕉

示例PHP:

  assert($ _ POST ['fruit'] === array(1 => ;'apple',2 =>'banana')); 

我对此有疑问:


  • 它背后的机制是什么?这些包含括号的名字在什么时候被转换为数组?这是HTTP协议的功能吗? Web服务器? PHP的语言是什么?


  • 继续前面的问题,这是一个常用的黑客或正常的编程工具吗? $ b

  • 在输入字段名称中使用括号的规则是什么(所有)?

  • 是否可以通过这种方式创建多维数组? / b>


解决方案


背后的机制是什么?在
点这个名称只包含
的括号转换为数组?
是HTPP协议的一个特性吗?
Web服务器?在PHP语言中?>


这是PHP语言的一项功能。实际上,HTTP协议并不禁止使用多个相同的GET / POST参数。根据HTTP规范,以下内容:

  foo = bar& foo = baz 

不应该导致 foo == baz 。这是两个具有两个不同值的参数。但是,PHP会用最新的 $ _ POST ['foo'] =='baz' foo c>,即使它们可以单独解析。


继续前面的问题,
是一种常用的hack或a正常的
编程工具?

这取决于观点。在PHP世界中,这是完全正常的,因为在不使用括号 [] 的情况下,语言不支持同名多个参数的规范。在HTTP世界中, foo!= foo []


什么是(全部)在输入字段名称中使用
括号的规则?

与PHP数组相同,不必引用字符串键。


可以创建多维数组
这种方式吗?


是的,您可以。


I've seen a lot of PHP code that handles form input in which the input field names contain square brackets. I understand that this somehow results in PHP arrays when a PHP script examines the $_POST variable.

Example HTML:

<form action='http://zzz.com' method='post'>
    <input name='fruit[1]' value='apple' />
    <input name='fruit[2]' value='banana' />
</form>

Example URL:

http://zzz.com?fruit[1]=apple&fruit[2]=banana

Example PHP:

assert($_POST['fruit'] === array(1=>'apple', 2=>'banana'));

My questions about this:

  • What is the mechanism behind it? At what point do these names that contain brackets get converted into arrays? Is this a feature of the HTTP protocol? Of web servers? Of the PHP language?

  • Continuing the previous question, is this a commonly used hack or a normal programming tool?

  • What are (all) the rules for using brackets in input field names?

  • Can multidimensional arrays be created this way?

解决方案

What is the mechanism behind? At which point this names that merely contain brackets are converted to arrays? Is this a feature of the HTPP protocol? Of web servers? Of the PHP language?>

This is a feature of the PHP language. In fact, the HTTP protocol does not forbid the use of multiple identical GET/POST parameters. According to the HTTP spec, the following:

foo=bar&foo=baz

Should not result in foo == baz. These are two parameters with two different values. However, PHP will overwrite the former foo with the latest, resulting in $_POST['foo'] == 'baz', even if they could be parsed separately.

Continuing the previous question, is this a commonly used hack or a normal programming tool?

It depends on the point of view. In the PHP world, it is completely normal, as the language does not support the specification of multiple parameters of the same name without using the brackets []. In the HTTP world though, foo != foo[].

What are (all) the rules of using brackets in input field names?

The same as PHP arrays, except that you don't have to quote string keys.

Can multidimensional arrays be created this way?

Yes, you can.

这篇关于包含方括号的表单输入字段名称,如字段[index]的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆