Html / PHP - 表单 - 输入为数组 [英] Html/PHP - Form - Input as array
问题描述
我有这样的表格
< form>
< input type =textclass =form-controlplaceholder =Titelname =levels [level]>
< input type =textclass =form-controlplaceholder =Titelname =levels [build_time]>
< input type =textclass =form-controlplaceholder =Titelname =levels [level]>
< input type =textclass =form-controlplaceholder =Titelname =levels [build_time]>
< / form>
我想要的$ _POST输出是一个像
$ b这样的数组数组([1] =>数组([level] => 1 [build_time] => 123)$ b $(
$ b pre $
b [2] => Array([level] => 2 [build_time] => 456)
)
我知道我可以做类似name =levels [1] [build_time]等等,但是因为这些元素是动态添加的,所以很难添加索引。有没有其他的办法?
编辑:
我的形式。我现在也包括了我的整个HTML,因为我觉得我在这里错过了一些东西。我的HTML现在:
< div class =form-group>
< label class =col-md-2>名称(z.B。1)< / label>
< div class =col-md-10>
< input type =textclass =form-controlplaceholder =Titelname =levels [] [level]>
< / div>
< label class =col-md-2> Bauzeit(In Sekunden)< / label>
< div class =col-md-10>
< input type =textclass =form-controlplaceholder =Titelname =levels [] [build_time]>
< / div>
< / div>
< div class =form-group>
< label class =col-md-2>名称(z.B。1)< / label>
< div class =col-md-10>
< input type =textclass =form-controlplaceholder =Titelname =levels [] [level]>
< / div>
< label class =col-md-2> Bauzeit(In Sekunden)< / label>
< div class =col-md-10>
< input type =textclass =form-controlplaceholder =Titelname =levels [] [build_time]>
< / div>
< / div>
我现在得到的输出是:
[levels] =>数组(
pre>
[0] =>数组([level] => 1)
[1] =>数组([build_time] => 234) > Array([level] => 2)
[3] => Array([build_time] => 456)
)
编辑2:
形成并将方括号移到名称的末尾。我现在得到的输出是:
[levels] => Array(
[level] => Array(
[0] => 1
[1] => 2
)
[build_time] => ;数组(
[0] => 234
[1] => 456
)
)
我猜这样做可能会有用,但它看起来很复杂。没有更好的办法吗?
< input type =textclass =form-controlplaceholder =Titelname =levels [level ] []>
< input type =textclass =form-controlplaceholder =Titelname =levels [build_time] []>
获取该模板,然后您可以使用循环添加这些模板。
然后,您可以根据需要动态添加这些内容,而不必提供索引。 PHP将会像你期望的场景例子一样提取它们。
编辑
对不起,我在错误的地方有大括号,这会使每个新值都成为一个新的数组元素。现在使用更新后的代码,这会给你以下数组结构:
levels> level(Array)
levels> build_time(Array)
两个子阵列上的相同索引会为您提供配对。例如
echo $ levels [level] [5];
echo $ levels [build_time] [5];
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
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?
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:
<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:
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?
解决方案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.
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];
这篇关于Html / PHP - 表单 - 输入为数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!