【问题标题】:HTML/PHP - Form - Input as arrayHTML/PHP - 表单 - 作为数组输入
【发布时间】:2013-12-09 16:23:45
【问题描述】:

我有这样的表格

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

按照您在编辑中的建议,我编辑了表单并将方括号移到了名称的末尾。我现在得到的输出是:

[levels] => Array (
  [level] => Array (
    [0] => 1
    [1] => 2
  )
  [build_time] => Array (
    [0] => 234
    [1] => 456
  )
)

我想这会奏效,但看起来仍然很复杂。没有更好的办法吗?

【问题讨论】:

    标签: php html html-input


    【解决方案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];
    

    【讨论】:

    • 抱歉,我的方括号放错了位置。请像我的新编辑一样将它们移到名称的末尾。
    • 我看到你再次编辑了你的帖子:) 我现在得到了相同的结构,但它仍然不是我最初想要的。这是最好的方法吗?
    • 这并不复杂。您可以访问一对像echo $levels["level"][5]; echo $levels["build_time"][5];
    • 这是 HTML 的一个非常棒的特性,在服务器端节省了大量的篡改。谢谢你的回答,对我帮助很大
    • 这太棒了。谢谢
    【解决方案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】:

      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】:

      另外:

      对于那些有一个空的$_POST 变量,不要使用这个:

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

      宁愿使用这个(因为它已经在这个例子中):

      name="levels[level][]"
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2018-01-29
        • 2015-08-29
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-01-26
        • 2012-11-21
        相关资源
        最近更新 更多