Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/http/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 如何格式化属于组的输入的名称_Php_Html Input - Fatal编程技术网

Php 如何格式化属于组的输入的名称

Php 如何格式化属于组的输入的名称,php,html-input,Php,Html Input,对不起,这个标题让人困惑,我已经尽力描述了我的问题 所以我有一些输入,以某种形式,我想把它们“组合”在一起,因为它们是同一实体的一部分 例如,当使用复选框时,我知道使用括号将它们放在一个数组中(通过PHP),但我的情况有点不同 我为一个人输入了3个输入,在表单中,可以有多个人 如果我的表格中只有一个人,情况就是这样: <input type="text" name="first_name"/> <input type="text" name="last_name"/> &

对不起,这个标题让人困惑,我已经尽力描述了我的问题

所以我有一些输入,以某种形式,我想把它们“组合”在一起,因为它们是同一实体的一部分

例如,当使用复选框时,我知道使用括号将它们放在一个数组中(通过PHP),但我的情况有点不同

我为一个
输入了3个输入,在表单中,可以有多个

如果我的表格中只有一个人,情况就是这样:

<input type="text" name="first_name"/>
<input type="text" name="last_name"/>
<input type="text" name="email"/>
我试过(1):


谢谢。

您必须明确地设置分组项目的索引。在您的情况下,它将是:

<form method="POST" action="">
    <input type="text" name="people[0][first_name]"/>
    <input type="text" name="people[0][last_name]"/>
    <input type="text" name="people[0][email]"/>
    <hr />

    <input type="text" name="people[1][first_name]"/>
    <input type="text" name="people[1][last_name]"/>
    <input type="text" name="people[1][email]"/>
    <hr />

    <input type="text" name="people[2][first_name]"/>
    <input type="text" name="people[2][last_name]"/>
    <input type="text" name="people[2][email]"/>
    <hr />

    <input type="submit" name="" value="" />
</form>




如果javascript添加了新字段,它们的名称也应该带有显式索引:

name="people[4][email]"
name="people[5][email]"
<!-- etc -->
name=“人员[4][email]”
name=“人员[5][电子邮件]”

变量1有什么问题?@u\u mulder我已经更新了我的问题,以显示变量1产生了什么(
print\r
-ing
$\u POST['people']
 <input type="text" name="people[first_name][]"/>
 <input type="text" name="people[last_name][]"/>
 <input type="text" name="people[email][]"/>
 <input type="text" name="people[][first_name][]"/>
 <input type="text" name="people[][last_name][]"/>
 <input type="text" name="people[][email][]"/>
Array
(
 [0] => Array
    (
        [first_name] => john
    )

[1] => Array
    (
        [last_name] => smith
    )

[2] => Array
    (
        [email] => john.smith@example.com
    )

[3] => Array
    (
        [first_name] => john2
    )

[4] => Array
    (
        [last_name] => smith2
    )

[5] => Array
    (
        [email] => john.smith@example.com2
    )

)
<form method="POST" action="">
    <input type="text" name="people[0][first_name]"/>
    <input type="text" name="people[0][last_name]"/>
    <input type="text" name="people[0][email]"/>
    <hr />

    <input type="text" name="people[1][first_name]"/>
    <input type="text" name="people[1][last_name]"/>
    <input type="text" name="people[1][email]"/>
    <hr />

    <input type="text" name="people[2][first_name]"/>
    <input type="text" name="people[2][last_name]"/>
    <input type="text" name="people[2][email]"/>
    <hr />

    <input type="submit" name="" value="" />
</form>
name="people[4][email]"
name="people[5][email]"
<!-- etc -->