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

<tfoot id='SYpyn'></tfoot>

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

    2. 在 foreach 中,isLastItem() 存在吗?

      时间:2023-09-23
        <bdo id='Ldm8G'></bdo><ul id='Ldm8G'></ul>

        <tfoot id='Ldm8G'></tfoot>

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

              1. 本文介绍了在 foreach 中,isLastItem() 存在吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                问题描述

                使用常规的 for 循环,可以将当前索引与最后一个进行比较,以判断我是否处于循环的最后一次迭代中.使用 foreach 时是否有类似的事情?我的意思是这样的.

                Using a regular for loop, it's possible to comapred the current index with the last to tell if I'm in the last iteration of the loop. Is there a similar thing when using foreach? I mean something like this.

                foreach($array as $item){
                  //do stuff
                  //then check if we're in the last iteration of the loop
                  $last_iteration = islast(); //boolean true/false
                }
                

                如果没有,是否至少有一种方法可以知道当前迭代的当前索引,例如 $iteration = 5,以便我可以手动将其与 $array 的长度进行比较?

                If not, is there at least a way to know the current index of the current iteration like $iteration = 5, so I can manually compare it to the length of the $array?

                推荐答案

                您可以组合使用 SPL 的 href="http://php.net/class.arrayiterator">ArrayIteratorCachingIterator 类有一个 hasNext 方法:

                You can use a combination of SPL’s ArrayIterator and CachingIterator class to have a hasNext method:

                $iter = new CachingIterator(new ArrayIterator($arr));
                foreach ($iter as $value) {
                    $last_iteration = !$iter->hasNext();
                }
                

                这篇关于在 foreach 中,isLastItem() 存在吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                上一篇:用 PHP 解析 JSON 数据 下一篇:使用 foreach 循环插入多个字段

                相关文章

                最新文章

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

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