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

    2. <small id='pOsVV'></small><noframes id='pOsVV'>

      PHP foreach by reference 在遍历对象数组时会导致奇怪

      时间:2023-09-23
    3. <legend id='cGTs1'><style id='cGTs1'><dir id='cGTs1'><q id='cGTs1'></q></dir></style></legend>
      <tfoot id='cGTs1'></tfoot>

          <tbody id='cGTs1'></tbody>

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

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

                本文介绍了PHP foreach by reference 在遍历对象数组时会导致奇怪的故障的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                问题描述

                我有一个对象数组.对象主要有一堆属性,因为这些是元数据对象.

                I have an array of objects. The objects mainly have a bunch of properties because these are meta-data objects.

                所以就像$objects[] 就像一堆具有以下属性的项目:object->item1object->item2

                so it is like $objects[] is like a bunch of items that have properties like: object->item1, object->item2, etc.

                我想为每个对象添加一些东西,所以...

                I want to add something to each of these objects, so...

                foreach ($objects as &$object) {
                  $object->newItem=(something I compute);
                }
                

                然后,我想将这些对象显示为 html 中的列表.所以,我去:

                then later, I want to display these objects as a list in html. So, I go:

                foreach ($objects as $object) {
                  <li><?php object output stuff here ?></li>
                }
                

                好的.现在,它工作正常,除了最后一个对象被丢弃并且倒数第二个对象显示两次.跆拳道??

                ok. Now, it works fine, except the last object is discarded and the second to last object is displayed twice. WTF??

                这对你有意义吗?

                推荐答案

                如果你通过引用迭代,之后总是取消设置迭代变量:

                If you iterate by reference, always unset the iteration variable afterwards:

                foreach ($objects as &$object) {
                      // some code
                }
                unset($object);
                

                摘自 foreach 文档:

                Excerpt from the foreach documentation:

                即使在 foreach 循环之后,对 $value 和最后一个数组元素的引用仍然存在.建议用unset()销毁.

                Reference of a $value and the last array element remain even after the foreach loop. It is recommended to destroy it by unset().

                如果您想了解为什么您的代码的行为方式如此,这里有一些进一步的阅读:引用和foreach

                If you want to understand why your code behaves the way it behaves, here is some further reading: References and foreach

                这篇关于PHP foreach by reference 在遍历对象数组时会导致奇怪的故障的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                上一篇:在 foreach 循环中什么更好...使用 &amp;符号或基 下一篇:PHP:限制 foreach() 语句?

                相关文章

                最新文章

                  • <bdo id='sRiC3'></bdo><ul id='sRiC3'></ul>
                  <legend id='sRiC3'><style id='sRiC3'><dir id='sRiC3'><q id='sRiC3'></q></dir></style></legend>
                  1. <small id='sRiC3'></small><noframes id='sRiC3'>

                    <tfoot id='sRiC3'></tfoot>

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