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

    2. <tfoot id='YztOz'></tfoot><legend id='YztOz'><style id='YztOz'><dir id='YztOz'><q id='YztOz'></q></dir></style></legend>

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

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

        如何在 foreach 循环中获取当前数组索引?

        时间:2023-09-23
          <i id='V4kzz'><tr id='V4kzz'><dt id='V4kzz'><q id='V4kzz'><span id='V4kzz'><b id='V4kzz'><form id='V4kzz'><ins id='V4kzz'></ins><ul id='V4kzz'></ul><sub id='V4kzz'></sub></form><legend id='V4kzz'></legend><bdo id='V4kzz'><pre id='V4kzz'><center id='V4kzz'></center></pre></bdo></b><th id='V4kzz'></th></span></q></dt></tr></i><div id='V4kzz'><tfoot id='V4kzz'></tfoot><dl id='V4kzz'><fieldset id='V4kzz'></fieldset></dl></div>
          <tfoot id='V4kzz'></tfoot>

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

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

                • 本文介绍了如何在 foreach 循环中获取当前数组索引?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  如何在 foreach 循环中获取当前索引?

                  How do I get the current index in a foreach loop?

                  foreach ($arr as $key => $val)
                  {
                      // How do I get the index?
                      // How do I get the first element in an associative array?
                  }
                  

                  推荐答案

                  在您的示例代码中,它只是 $key.

                  In your sample code, it would just be $key.

                  例如,如果您想知道这是循环的第一次、第二次或 ith 次迭代,这是您唯一的选择:

                  If you want to know, for example, if this is the first, second, or ith iteration of the loop, this is your only option:

                  $i = -1;
                  foreach($arr as $val) {
                    $i++;
                    //$i is now the index.  if $i == 0, then this is the first element.
                    ...
                  }
                  

                  当然,这并不意味着 $val == $arr[$i] 因为数组可以是关联数组.

                  Of course, this doesn't mean that $val == $arr[$i] because the array could be an associative array.

                  这篇关于如何在 foreach 循环中获取当前数组索引?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:PHP - 使用 foreach 抓取第一个元素 下一篇:如何遍历php中的对象

                  相关文章

                  最新文章

                  <tfoot id='Da9Pv'></tfoot>

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

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

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