HTML/PHP - 양식 - 배열로 입력
저는 이런 양식을 받았습니다.
<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="float[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][]"
언급URL : https://stackoverflow.com/questions/20184670/html-php-form-input-as-array
'programing' 카테고리의 다른 글
my gitlab-ci에서 주상 mariadb 서비스를 추가하는 방법 (0) | 2023.09.06 |
---|---|
Powershell 스크립트를 사용하여 IISRESET을 수행하는 방법 (0) | 2023.09.06 |
스판이나 디브 포장 방지 (0) | 2023.09.06 |
큰 관계 하나와 작은 관계 세 개 중 어느 것이 더 나을까요? (0) | 2023.09.06 |
텍스트가 swift UI로 감싸지지 않습니다. (0) | 2023.09.06 |