HTML/PHP - 表单 - 作为数组输入

Posted

技术标签:

【中文标题】HTML/PHP - 表单 - 作为数组输入【英文标题】:HTML/PHP - Form - Input as array 【发布时间】:2013-12-09 16:23:45 【问题描述】:

我有这样的表格

<form>
    <input type="text" class="form-control" placeholder="Titel" name="levels[level]">
    <input type="text" class="form-control" placeholder="Titel" name="levels[build_time]">

    <input type="text" class="form-control" placeholder="Titel" name="levels[level]">
    <input type="text" class="form-control" placeholder="Titel" name="levels[build_time]">
</form>

我希望 $_POST 输出一个数组,如:

Array (
  [1] => Array ( [level] => 1 [build_time] => 123 )
  [2] => Array ( [level] => 2 [build_time] => 456 )
)

我知道我可以执行诸如 name="levels[1][build_time]" 之类的操作,但是由于这些元素是动态添加的,因此很难添加索引。还有其他方法吗?


按照建议,我更改了表格。我现在还包含了我的整个 html,因为我认为我在这里遗漏了一些东西。我现在的 HTML:

<div class="form-group">
  <label class="col-md-2">Name(z.B. 1)</label>
  <div class="col-md-10">
    <input type="text" class="form-control" placeholder="Titel" name="levels[][level]">
  </div>

  <label class="col-md-2">Bauzeit(In Sekunden)</label>
  <div class="col-md-10">
    <input type="text" class="form-control" placeholder="Titel" name="levels[][build_time]">
  </div>
</div>

<div class="form-group">
  <label class="col-md-2">Name(z.B. 1)</label>
  <div class="col-md-10">
    <input type="text" class="form-control" placeholder="Titel" name="levels[][level]">
  </div>

  <label class="col-md-2">Bauzeit(In Sekunden)</label>
  <div class="col-md-10">
    <input type="text" class="form-control" placeholder="Titel" name="levels[][build_time]">
  </div>
</div>

我现在得到的输出是:

[levels] => Array (
  [0] => Array ( [level] => 1 )
  [1] => Array ( [build_time] => 234 )
  [2] => Array ( [level] => 2 )
  [3] => Array ( [build_time] => 456 )
)

按照您在编辑中的建议,我编辑了表单并将方括号移到了名称的末尾。我现在得到的输出是:

[levels] => Array (
  [level] => Array (
    [0] => 1
    [1] => 2
  )
  [build_time] => Array (
    [0] => 234
    [1] => 456
  )
)

我想这会奏效,但看起来仍然很复杂。没有更好的办法吗?

【问题讨论】:

【参考方案1】:

只需将[] 添加到类似的名称中

 <input type="text" class="form-control" placeholder="Titel" name="levels[level][]">
 <input type="text" class="form-control" placeholder="Titel" name="levels[build_time][]">

获取该模板,然后您甚至可以使用循环添加它们。

然后您可以根据需要动态添加它们,而无需提供索引。 php 会像您预期的场景示例一样拾取它们。

编辑

对不起,我把大括号放在了错误的位置,这会使每个新值都成为一个新的数组元素。现在使用更新的代码,这将为您提供以下数组结构

levels > level (Array)
levels > build_time (Array)

两个子数组上的相同索引将为您提供一对。例如

echo $levels["level"][5];
echo $levels["build_time"][5];

【讨论】:

抱歉,我的方括号放错了位置。请像我的新编辑一样将它们移到名称的末尾。 我看到你再次编辑了你的帖子:) 我现在得到了相同的结构,但它仍然不是我最初想要的。这是最好的方法吗? 这并不复杂。您可以访问一对像echo $levels["level"][5]; echo $levels["build_time"][5]; 这是 HTML 的一个非常棒的特性,在服务器端节省了大量的篡改。谢谢你的回答,对我帮助很大 这太棒了。谢谢【参考方案2】:

如果你可以索引数组,你可以这样做:

<form>
    <input type="text" class="form-control" placeholder="Titel" name="levels[0][level]">
    <input type="text" class="form-control" placeholder="Titel" name="levels[0][build_time]">

    <input type="text" class="form-control" placeholder="Titel" name="levels[1][level]">
    <input type="text" class="form-control" placeholder="Titel" name="levels[1][build_time]">

    <input type="text" class="form-control" placeholder="Titel" name="levels[2][level]">
    <input type="text" class="form-control" placeholder="Titel" name="levels[2][build_time]">
</form>

...实现这一点:

[levels] => Array (
  [0] => Array (
    [level] => 1
    [build_time] => 2
  )
  [1] => Array (
    [level] => 234
   [build_time] => 456
  )
  [2] => Array (
    [level] => 111
    [build_time] => 222
  )
)

但是,如果您从表单中间删除一对输入(我想是动态的),那么您的数组中就会出现空洞,除非您更新输入名称...

【讨论】:

【参考方案3】:

HTML:使用名称作为

<input name="levels[level][]">
<input name="levels[build_time][]">

PHP:

$array = filter_input_array(INPUT_POST);
$newArray = array();
foreach (array_keys($array) as $fieldKey) 
    foreach ($array[$fieldKey] as $key=>$value) 
        $newArray[$key][$fieldKey] = $value;
    
  

$newArray 将根据需要保存数据

Array ( 
  [0] => Array ( [level] => 1 [build_time] => 123 ) 
  [1] => Array ( [level] => 2 [build_time] => 456 )
)

【讨论】:

我不知道为什么这被否决了,但这正是我所需要的。【参考方案4】:

另外:

对于那些有一个空的$_POST 变量,不要使用这个:

name="[levels][level][]"

宁愿使用这个(因为它已经在这个例子中):

name="levels[level][]"

【讨论】:

以上是关于HTML/PHP - 表单 - 作为数组输入的主要内容,如果未能解决你的问题,请参考以下文章

将html表单中的数据插入sql数据库(html,php,sql)时未保存输入的值

使用 jQuery 将表单数据作为数组传递给 Laravel

如何将Javascript数组作为Json与html表单结合发送?

来自数组 JS 的自动完成输入表单

表单输入数组到 MySQL DB

javascript 收集输入字段数据并使用key作为输入名称和值作为输入值构建json。提供HTML表单作为参数