<legend id='gdRFv'><style id='gdRFv'><dir id='gdRFv'><q id='gdRFv'></q></dir></style></legend>
  • <i id='gdRFv'><tr id='gdRFv'><dt id='gdRFv'><q id='gdRFv'><span id='gdRFv'><b id='gdRFv'><form id='gdRFv'><ins id='gdRFv'></ins><ul id='gdRFv'></ul><sub id='gdRFv'></sub></form><legend id='gdRFv'></legend><bdo id='gdRFv'><pre id='gdRFv'><center id='gdRFv'></center></pre></bdo></b><th id='gdRFv'></th></span></q></dt></tr></i><div id='gdRFv'><tfoot id='gdRFv'></tfoot><dl id='gdRFv'><fieldset id='gdRFv'></fieldset></dl></div>

    <tfoot id='gdRFv'></tfoot>
  • <small id='gdRFv'></small><noframes id='gdRFv'>

      <bdo id='gdRFv'></bdo><ul id='gdRFv'></ul>

        获取 foreach 循环中的下一个元素

        时间:2023-09-23

        • <bdo id='FxE35'></bdo><ul id='FxE35'></ul>
          <legend id='FxE35'><style id='FxE35'><dir id='FxE35'><q id='FxE35'></q></dir></style></legend>
            <tbody id='FxE35'></tbody>

          <small id='FxE35'></small><noframes id='FxE35'>

                <i id='FxE35'><tr id='FxE35'><dt id='FxE35'><q id='FxE35'><span id='FxE35'><b id='FxE35'><form id='FxE35'><ins id='FxE35'></ins><ul id='FxE35'></ul><sub id='FxE35'></sub></form><legend id='FxE35'></legend><bdo id='FxE35'><pre id='FxE35'><center id='FxE35'></center></pre></bdo></b><th id='FxE35'></th></span></q></dt></tr></i><div id='FxE35'><tfoot id='FxE35'></tfoot><dl id='FxE35'><fieldset id='FxE35'></fieldset></dl></div>

                  <tfoot id='FxE35'></tfoot>

                  本文介绍了获取 foreach 循环中的下一个元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  我有一个 foreach 循环,我想查看循环中是否有下一个元素,以便我可以将当​​前元素与下一个元素进行比较.我怎样才能做到这一点?我已经阅读了 current 和 next 函数,但我不知道如何使用它们.

                  I have a foreach loop and I want to see if there is a next element in the loop so I can compare the current element with the next. How can I do this? I've read about the current and next functions but I can't figure out how to use them.

                  提前致谢

                  推荐答案

                  一种独特的方法是反转数组和 then 循环.这也适用于非数字索引数组:

                  A unique approach would be to reverse the array and then loop. This will work for non-numerically indexed arrays as well:

                  $items = array(
                      'one'   => 'two',
                      'two'   => 'two',
                      'three' => 'three'
                  );
                  $backwards = array_reverse($items);
                  $last_item = NULL;
                  
                  foreach ($backwards as $current_item) {
                      if ($last_item === $current_item) {
                          // they match
                      }
                      $last_item = $current_item;
                  }
                  

                  如果您仍然对使用 currentnext 函数感兴趣,您可以这样做:

                  If you are still interested in using the current and next functions, you could do this:

                  $items = array('two', 'two', 'three');
                  $length = count($items);
                  for($i = 0; $i < $length - 1; ++$i) {
                      if (current($items) === next($items)) {
                          // they match
                      }
                  }
                  

                  #2 可能是最好的解决方案.注意,$i <$length - 1; 将在比较数组中的最后两项后停止循环.我把它放在循环中,以便在示例中明确.你应该只计算 $length = count($items) - 1;

                  #2 is probably the best solution. Note, $i < $length - 1; will stop the loop after comparing the last two items in the array. I put this in the loop to be explicit with the example. You should probably just calculate $length = count($items) - 1;

                  这篇关于获取 foreach 循环中的下一个元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:带有 HTML 的 PHP 简单 foreach 循环 下一篇:限制循环在 php 中运行的次数

                  相关文章

                  最新文章

                1. <small id='W1i87'></small><noframes id='W1i87'>

                    <tfoot id='W1i87'></tfoot>

                    <i id='W1i87'><tr id='W1i87'><dt id='W1i87'><q id='W1i87'><span id='W1i87'><b id='W1i87'><form id='W1i87'><ins id='W1i87'></ins><ul id='W1i87'></ul><sub id='W1i87'></sub></form><legend id='W1i87'></legend><bdo id='W1i87'><pre id='W1i87'><center id='W1i87'></center></pre></bdo></b><th id='W1i87'></th></span></q></dt></tr></i><div id='W1i87'><tfoot id='W1i87'></tfoot><dl id='W1i87'><fieldset id='W1i87'></fieldset></dl></div>

                    1. <legend id='W1i87'><style id='W1i87'><dir id='W1i87'><q id='W1i87'></q></dir></style></legend>
                        <bdo id='W1i87'></bdo><ul id='W1i87'></ul>