【问题标题】:How to get only one result from foreach(PHP)如何从foreach(PHP)中只获得一个结果
【发布时间】:2011-10-07 15:45:16
【问题描述】:

代码循环一个数组并为用户显示所有视图。现在事情发生了变化,我只需要显示一个 foreach 循环的结果。我该怎么做?

<table class="report_edits_table">
<thead>
  <tr class="dates_row">
  <?php foreach($report['edits'] as $report_edit) : ?>
    <td colspan="2" report_edit_id="<?php echo $report_edit['id'] ?>"><div class="date_container">
    <?php if($sf_user->hasCredential(Attribute::COACHING_EDIT_ACCESS)) : ?>
      <span class="ui-icon ui-icon-trash">Remove</span>
    <?php endif?>
    <?php echo "View " . link_to($report_edit['created'], sprintf('coaching/viewReportEdit?reportedit=%s', $report_edit['id']), array('title' => 'View This Contact')) ?> </div></td>
  <?php endforeach ?>
  </tr>
</thead>
<tbody>
  <?php foreach($report['edits_titles'] as $index => $title) : ?>
  <tr class="coach_row">
    <?php for ($i=max(0, count($report['edits'])-2); $i<count($report['edits']); $i++) : $report_edit = $report['edits'][$i] ?>
    <td class="name_column"><?php echo $title ?></td>
    <td class="value_column"><?php echo $report_edit[$index] ?></td>
    <?php endfor ?>
  </tr>
  <?php endforeach ?>
</tbody>

【问题讨论】:

  • 我真的不知道你想做什么,但你总是可以break;跳出foreach()

标签: php


【解决方案1】:

使用break命令进行简单转换:

<?php for ... ?>
    ... stuff here ...
    <?php break; ?>
<?php endfor ... ?>

更好的解决方案是完全删除foreach

【讨论】:

    【解决方案2】:

    听起来您想从数组中获取第一个元素,而不必遍历其余元素。

    PHP 为此类情况提供了一组函数。

    要获取数组中的第一个元素,首先使用reset() 函数将数组指针定位到数组的开头,然后使用current() 函数读取指针正在查看的元素。

    所以你的代码应该是这样的:

    <?php
    reset($report['edits']);
    $report_edit = current($report['edits']);
    ?>
    

    现在您可以使用$report_edits,而无需使用foreach() 循环。

    (请注意,数组指针实际上默认从第一条记录开始,因此您可以跳过reset() 调用,但最好不要这样做,因为它可能已被更改在你没有意识到的情况下在你的代码中的其他地方)

    如果您想在此之后继续下一条记录,可以使用next() 函数。如您所见,如果您愿意,理论上可以使用这些函数编写另一种类型的foreach() 循环。以这种方式使用它们没有任何意义,但这是可能的。但是它们确实允许对数组进行更细粒度的控制,这对于像您这样的情况很方便。

    希望对您有所帮助。

    【讨论】:

      【解决方案3】:

      方法很多

      1. 直接访问相关数组元素的索引
      2. 更新任何逻辑获取/生成数组以仅返回感兴趣的元素
      3. 使用在单个循环后终止的 for 循环
      4. array_filter 数组以获取感兴趣的元素
      5. 在 foreach 循环结束时中断,使其在第一次迭代后终止
      6. 有条件地在 foreach 循环中检查您的索引,并且仅在索引与感兴趣的元素匹配时才输出标记
      7. 等等

      我建议只获取感兴趣的数组元素(列表中的第 2 位),因为这意味着在您的代码周围反弹的数据更少(如果您从 SQL 填充数组,则可能在您的 PHP 框和数据库之间反弹)服务器)

      【讨论】:

        【解决方案4】:

        最简单的方法?

        break 作为 foreach 的最后一行。它会执行一次,然后退出。 (只要你停在哪个元素上并不重要)。

        第二种方法:在您的$report['edits']$report['edits_titles'] 上使用array_poparray_shift 来获取元素,丢失for 循环,并引用您刚刚检索到的元素。

        例如:

        //
        // current
        //
        foreach ($report['edits'] as $report_edit) :
          /* markup */
        endforeach;
        
        //
        // modified version
        //
        $report_edit = array_shift($report['edits']);
          /* markup */
        

        【讨论】:

          【解决方案5】:

          &lt;?php endforeach ?&gt;之前使用&lt;?php break ?&gt;

          【讨论】:

            【解决方案6】:
            example :: 
            
            <?php  
              foreach ($this->oFuelData AS $aFuelData ) {
                echo $aFuelData['vehicle'];
                break;                      
              }
            ?>
            

            【讨论】:

              【解决方案7】:

              你也可以参考一下

                  foreach($array as $element) {
                  if ($element === reset($array))
                      echo $element;
              
                  if ($element === end($array))
                      echo $element;
                  }
              

              【讨论】:

                猜你喜欢
                • 2018-06-01
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                • 2016-04-29
                • 1970-01-01
                • 2020-08-22
                • 1970-01-01
                • 1970-01-01
                相关资源
                最近更新 更多