<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)
)
編輯2:
在你編輯的建議,我編輯的形式和移動的方括號名字的結尾。我現在得到的輸出是:
[levels] => Array (
[level] => Array (
[0] => 1
[1] => 2
)
[build_time] => Array (
[0] => 234
[1] => 456
)
)
我想這會有點兒工作,但它仍然看起來複雜。沒有更好的方法?
請看我的編輯,我做錯了什麼? –
抱歉抱歉,我的方括號在錯誤的地方。請將它們移動到名稱末尾,例如我的新編輯。 –
我看到你再次編輯你的文章:)我現在得到了相同的結構,但它仍然不是我最初想要的。這是做到這一點的最佳方式? –