Html/PHP - 表单 - 作为数组输入
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20184670/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Html/PHP - Form - Input as array
提问by Evo_x
I got a form like this
我有一个这样的表格
<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>
What I'd like to have as $_POST output is an array like
我想要作为 $_POST 输出的是一个数组
Array (
[1] => Array ( [level] => 1 [build_time] => 123 )
[2] => Array ( [level] => 2 [build_time] => 456 )
)
I know I could do something like name="levels[1][build_time]" and so on but since these elements get added dynamically, it would be hard to add an index. Is there any other way?
我知道我可以执行诸如 name="levels[1][build_time]" 之类的操作,但由于这些元素是动态添加的,因此很难添加索引。有没有其他办法?
EDIT:
编辑:
As suggested, I changed my form. I also included my whole HTML now, because I think I'm missing something here. My HTML now:
按照建议,我改变了我的形式。我现在还包括了我的整个 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>
The output I get now is:
我现在得到的输出是:
[levels] => Array (
[0] => Array ( [level] => 1 )
[1] => Array ( [build_time] => 234 )
[2] => Array ( [level] => 2 )
[3] => Array ( [build_time] => 456 )
)
Edit 2:
编辑2:
As suggested in your edit, I edited my form and moved the square brackets to the end of the name. The output I get now is:
正如您在编辑中所建议的那样,我编辑了我的表单并将方括号移到名称的末尾。我现在得到的输出是:
[levels] => Array (
[level] => Array (
[0] => 1
[1] => 2
)
[build_time] => Array (
[0] => 234
[1] => 456
)
)
I guess that would kinda work but it still looks complicated. No better way?
我想这会有点工作,但它看起来仍然很复杂。没有更好的办法吗?
回答by Hanky Panky
Simply add []to those names like
只需添加[]到这些名称中,例如
<input type="text" class="form-control" placeholder="Titel" name="levels[level][]">
<input type="text" class="form-control" placeholder="Titel" name="levels[build_time][]">
Take that template and then you can add those even using a loop.
获取该模板,然后您甚至可以使用循环添加这些模板。
Then you can add those dynamically as much as you want, without having to provide an index. PHP will pick them up just like your expected scenario example.
然后,您可以根据需要动态添加这些内容,而无需提供索引。PHP 会像您预期的场景示例一样选择它们。
Edit
编辑
Sorry I had braces in the wrong place, which would make every new value as a new array element. Use the updated code now and this will give you the following array structure
对不起,我在错误的地方有大括号,这会使每个新值都作为新的数组元素。现在使用更新的代码,这将为您提供以下数组结构
levels > level (Array)
levels > build_time (Array)
Same index on both sub arrays will give you your pair. For example
两个子数组上的相同索引将为您提供您的配对。例如
echo $levels["level"][5];
echo $levels["build_time"][5];
回答by Nuno Rafael Figueiredo
If is ok for you to index the array you can do this:
如果你可以索引数组,你可以这样做:
<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>
... to achieve that:
... 实现这一目标:
[levels] => Array (
[0] => Array (
[level] => 1
[build_time] => 2
)
[1] => Array (
[level] => 234
[build_time] => 456
)
[2] => Array (
[level] => 111
[build_time] => 222
)
)
But if you remove one pair of inputs (dynamically, I suppose) from the middle of the form then you'll get holes in your array, unless you update the input names...
但是,如果您从表单中间删除一对输入(我想是动态的),那么您的数组中就会出现漏洞,除非您更新输入名称...
回答by user6542662
HTML: Use names as
HTML:使用名称作为
<input name="levels[level][]">
<input name="levels[build_time][]">
PHP:
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 will hold data as you want
$newArray 将根据需要保存数据
Array (
[0] => Array ( [level] => 1 [build_time] => 123 )
[1] => Array ( [level] => 2 [build_time] => 456 )
)
回答by mheg
in addition: for those who have a empty POST variable, don't use this:
另外:对于那些有空 POST 变量的人,不要使用这个:
name="[levels][level][]"
rather use this (as it is already here in this example):
而是使用它(因为它已经在这个例子中):
name="levels[level][]"

