복붙노트

HTML / PHP - 양식 - 배열로 입력

PHP

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 = "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 
  )
) 

나는 그것이 일하는 것 같지만 여전히 복잡해 보입니다. 더 나은 방법이 없습니까?

해결법

  1. ==============================

    1.같은 이름에 []를 추가하기 만하면됩니다.

    같은 이름에 []를 추가하기 만하면됩니다.

     <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];
    
  2. ==============================

    2.배열을 색인 할 수 있다면 다음과 같이 할 수 있습니다 :

    배열을 색인 할 수 있다면 다음과 같이 할 수 있습니다 :

    <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 
      )
    ) 
    

    하지만 폼의 중간에서 한 쌍의 입력을 제거한다면 (다이내믹 한 것 같습니다), 입력 이름을 업데이트하지 않으면 배열에 구멍이 생깁니다.

  3. ==============================

    3.HTML : 이름을 다음과 같이 사용하십시오.

    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 )
    )
    
  4. ==============================

    4.게다가: 빈 POST 변수가있는 사용자는 다음을 사용하지 마십시오.

    게다가: 빈 POST 변수가있는 사용자는 다음을 사용하지 마십시오.

    name="[levels][level][]"
    

    오히려 이것을 사용하십시오 (이 예에서는 이미 있습니다).

    name="levels[level][]"
    
  5. from https://stackoverflow.com/questions/20184670/html-php-form-input-as-array by cc-by-sa and MIT license