【问题标题】:Iterating multiple $_POST arrays迭代多个 $_POST 数组
【发布时间】:2010-09-27 20:10:13
【问题描述】:

我有以下代码:

<tr>
    <td width="60%">
        <dl>
            <dt>Full Name</dt>
            <dd>
                <input name="fullname[]" type="text" class="txt w90" id="fullname[]" value="<?php echo $value; ?>" />
            </dd>
        </dl>
    </td>
    <td width="30%">
        <dl>
            <dt>Job Title</dt>
            <dd>
                <input name="job_title[]" type="text" class="txt w90" id="job_title[]" value="<?php echo $value2; ?>" />
            </dd>
        </dl>
    </td>
</tr>

假设我有几行上述代码。如何迭代并获取数组 $_POST['fullname']$_POST['job_title'] 的值?

【问题讨论】:

    标签: php arrays input multidimensional-array


    【解决方案1】:

    只是一个数组:

    foreach ($_POST['fullname'] as $name) {
        echo $name."\n";
    }
    

    如果问题是您想并行遍历两个数组,只需使用其中一个来获取索引:

    for ($i=0; $i < count($_POST['fullname']); $i++) {
        echo $_POST['fullname'][$i]."\n";
        echo $_POST['job_title'][$i]."\n";
    }
    

    【讨论】:

      【解决方案2】:

      我早些时候删除了这个,因为它非常接近 Vinko 的答案。

      for ($i = 0, $t = count($_POST['fullname']); $i < $t; $i++) {
          $fullname = $_POST['fullname'][$i];
          $job_title = $_POST['job_title'][$i];
          echo "$fullname $job_title \n";
      }
      

      原始索引不是从 0 - N-1 的数字

      $range = array_keys($_POST['fullname']);
      foreach ($range as $key) {
          $fullname = $_POST['fullname'][$key];
          $job_title = $_POST['job_title'][$key];
          echo "$fullname $job_title \n";
      }
      

      这只是一般信息。使用 SPL DualIterator,您可以进行以下操作:

      $dualIt = new DualIterator(new ArrayIterator($_POST['fullname']), new ArrayIterator($_POST['job_title']));
      
      while($dualIt->valid()) {
          list($fullname, $job_title) = $dualIt->current();
          echo "$fullname $job_title \n";
          $dualIt->next();
      }
      

      【讨论】:

      • 那么 DualIterator 的好处是什么?
      • 它与 SPL 中的所有其他迭代器相似,因此对于使用迭代器的人来说很熟悉。我猜它对于那些习惯于 Java 的人来说也很相似。但我认为他们至少应该制作一个 MultiIterator 而不是有限的 DualIterator。
      【解决方案3】:

      我认为您要解决的问题是从具有相同索引的 $_POST['fullname'][] 和 $_POST['jobtitle'][] 中获取一对值。

      for ($i = 0, $rowcount = count($_POST['fullname']); $i < $rowcount; $i++)
      {
          $name = $_POST['fullname'][$i]; // get name
          $job  = $_POST['jobtitle'][$i]; // get jobtitle
      }
      

      【讨论】:

      • 这个答案和上面OIS的答案一样。
      • 是的,我在发布答案后看到了。
      【解决方案4】:

      如果我理解正确,您有 2 个数组,您基本上希望并行迭代。

      以下内容可能对您有用。您可以使用$_POST['fullname']$_POST['jobtitle'],而不是$a1$a2

      <?php
      $a1=array('a','b','c','d','e','f');
      $a2=array('1','2','3','4','5','6');
      
      // reset array pointers    
      reset($a1); reset($a2);
      while (TRUE)
      {
        // get current item
        $item1=current($a1);
        $item2=current($a2);
        // break if we have reached the end of both arrays
        if ($item1===FALSE and $item2===FALSE) break;  
        print $item1.' '. $item2.PHP_EOL;
        // move to the next items
        next($a1); next($a2);
      }
      

      【讨论】:

        【解决方案5】:

        Vinko 和 OIS 的答案都非常好(我提高了 OIS')。但是,如果您总是打印 5 个文本字段副本,您总是可以专门为每个字段命名:

        <?php $i=0; while($i < 5) { ?><tr>
            ...
            <input name="fullname[<?php echo $i; ?>]" type="text" class="txt w90" id="fullname[<?php echo $i; ?>]" value="<?php echo $value; ?>" />
        

        【讨论】:

        • 这将产生与不命名它们相同的结果。
        • 不完全是。用户可以在 name[1] 和 job[3] 中输入数据并点击提交。空数组将返回 name[0] 和 job[0]。我的不会。 (我不知道这对我有多大帮助。:-))
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2018-02-14
        • 1970-01-01
        • 1970-01-01
        • 2023-03-08
        • 2014-05-17
        • 1970-01-01
        相关资源
        最近更新 更多