PHP HTML数据格式
如何处理此类输入表单:PHP HTML数据格式,php,html,symfony,Php,Html,Symfony,如何处理此类输入表单: <form action="update" method="post"> <table> <tr> <th>First name</th> <th>Last name</th> </tr> <tr> <td><input
<form action="update" method="post">
<table>
<tr>
<th>First name</th>
<th>Last name</th>
</tr>
<tr>
<td><input type="text" name="people[][firstname]" value="John" /></td>
<td><input type="text" name="people[][surname]" value="Smith" /></td>
</tr>
<tr>
<td><input type="text" name="people[][firstname]" value="Adam" /></td>
<td><input type="text" name="people[][surname]" value="Boralsky" /></td>
</tr>
</table>
</form>
有人见过这种格式吗?这种情况正在发生,因为这实际上是您要求PHP做的事情。[]是“下一个数组索引”操作符,因此当PHP处理输入时,它会看到
people[][firstname]=John
,将其理解为“Add to$people[]=['firstname'=>'John']”,您会得到:
$people = [
['firstname' => 'John'],
];
然后,当我们得到people[][姓氏]=Smith
,“添加到$people[]=[姓氏]=>“Smith']”。这就给了我们:
$people = [
['firstname' => 'John'],
['Surname' => 'Smith'],
];
等等
您需要做的是在html中显式设置一个数组键。如果这是表单中唯一的人,则使用
people[0]
而不是people[]
。如果有多个人,那么您必须为一个人共有的每个字段动态生成索引。将索引改为people[0][firstname]
,people[0][lasname]
等等,它似乎是一个PHP数组。你在哪里收到这个?如何打印此阵列?你是如何在数组中访问这些值的?赢家!写得好,先生。
$people = [
['firstname' => 'John'],
['Surname' => 'Smith'],
];