HTML/PHP-Form-以数组形式输入


HTML/PHP - Form - Input as array

我得到了这样的表格

<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
  )
)

我想这是可行的,但看起来仍然很复杂。难道没有更好的方法吗?

只需将[]添加到等名称中

 <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];

如果可以对数组进行索引,则可以执行以下操作:

<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
  )
)

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

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 )
)

此外:

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

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

相反,使用这个(在这个例子中已经有了):

name="levels[level][]"