Html / PHP-Form-Input as array
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>
To, co chciałbym mieć jako wyjście $_POST, to tablica typu
Array (
[1] => Array ( [level] => 1 [build_time] => 123 )
[2] => Array ( [level] => 2 [build_time] => 456 )
)
Wiem, że mógłbym zrobić coś w rodzaju name="levels [1] [build_time]" i tak dalej, ale ponieważ te elementy są dodawane dynamicznie, trudno byłoby dodać indeks. Jest jakiś inny sposób?
EDIT:
Zgodnie z sugestią zmieniłem formę. Włączyłem też teraz cały HTML, ponieważ myślę, że coś mi tu umyka. Mój HTML teraz:<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>
Wyjście I get now is:
[levels] => Array (
[0] => Array ( [level] => 1 )
[1] => Array ( [build_time] => 234 )
[2] => Array ( [level] => 2 )
[3] => Array ( [build_time] => 456 )
)
Edytuj 2:
Zgodnie z sugestią w Twojej edycji, edytowałem mój formularz i przeniosłem nawiasy kwadratowe na koniec nazwy. Wyjście, które teraz otrzymuję, to:
[levels] => Array (
[level] => Array (
[0] => 1
[1] => 2
)
[build_time] => Array (
[0] => 234
[1] => 456
)
)
To by chyba zadziałało, ale i tak wygląda na skomplikowane. Nie ma lepszego sposobu? 4 answers
Wystarczy dodać []
do takich nazw jak
<input type="text" class="form-control" placeholder="Titel" name="levels[level][]">
<input type="text" class="form-control" placeholder="Titel" name="levels[build_time][]">
Weź ten szablon, a następnie możesz dodać je nawet za pomocą pętli.
Następnie możesz dodawać je dynamicznie, ile chcesz, bez konieczności podawania indeksu. PHP odbierze je tak, jak oczekiwany przykład scenariusza.
Edit
Przepraszam, że miałem klamry w niewłaściwym miejscu, co sprawiłoby, że każda nowa wartość byłaby nowym elementem tablicy. Użyj teraz zaktualizowanego kodu, a otrzymasz następującą tablicę struktura
levels > level (Array)
levels > build_time (Array)
Ten sam indeks na obu tablicach podrzędnych da ci twoją parę. Na przykład
echo $levels["level"][5];
echo $levels["build_time"][5];
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2013-11-25 04:36:44
Jeśli możesz indeksować tablicę, możesz to zrobić:
<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>
... aby to osiągnąć:
[levels] => Array (
[0] => Array (
[level] => 1
[build_time] => 2
)
[1] => Array (
[level] => 234
[build_time] => 456
)
[2] => Array (
[level] => 111
[build_time] => 222
)
)
Ale jeśli usuniesz jedną parę wejść (dynamicznie, jak sądzę) ze środka formularza, to dostaniesz dziury w tablicy, chyba że zaktualizujesz nazwy wejść...
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2015-03-17 14:16:41
HTML: używaj nazw jako
<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 przechowuje dane tak, jak chcesz
Array (
[0] => Array ( [level] => 1 [build_time] => 123 )
[1] => Array ( [level] => 2 [build_time] => 456 )
)
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2016-07-02 22:55:52
Dodatkowo: dla tych, którzy mają pustą zmienną POST, nie używaj tego:
name="[levels][level][]"
Raczej użyj tego (jak to jest już tutaj w tym przykładzie):
name="levels[level][]"
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2014-09-22 09:56:00