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

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

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

    2. <tfoot id='HFkcF'></tfoot>
    3. 将实现 Iterator 的 PHP 类视为数组

      时间:2023-09-23

            <tbody id='iVSJc'></tbody>
        • <small id='iVSJc'></small><noframes id='iVSJc'>

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

              <legend id='iVSJc'><style id='iVSJc'><dir id='iVSJc'><q id='iVSJc'></q></dir></style></legend>
              • <tfoot id='iVSJc'></tfoot>

                本文介绍了将实现 Iterator 的 PHP 类视为数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                问题描述

                如果我有一个实现 Iterator 接口的类,我可以手动控制 foreach 循环中的迭代方式.但是还有其他方法可以让我的对象表现得像一个数组吗?

                If I have a class that implements the Iterator interface, I can manually control how iteration in a foreach loop. But are there other ways in which I could make my object behave like an array?

                例如,假设我有一个实现 Iterator 的类 Guestbook,这样我就可以迭代 foreach (new Guestbook() as $entry).但是,如果我想颠倒顺序怎么办?

                For instance, let's say I have a class Guestbook which implements Iterator, so that I can iterate foreach (new Guestbook() as $entry). But what if I want to, say, reverse the order?

                foreach (array_reverse(new Guestbook()) as $entry) 肯定不行,因为 array_reverse 只接受一个数组.

                foreach (array_reverse(new Guestbook()) as $entry) definitely won't work, because array_reverse will only accept an array.

                我想我想问的是,我可以将 Iterator 用于更多的 foreach 循环吗?

                I guess what I'm asking is, can I use Iterator for more than just foreach loops?

                谢谢.

                推荐答案

                迭代器接口的目的 是为了让你的对象在 foreach 循环中使用,它不是为了让你的对象像一个数组.如果您想要一些像数组一样的东西,请使用数组.

                The purpose of the Iterator interface is to allow your object to be used in a foreach loop, it is not intended to make your object act like an array. If you want something that acts like an array, use an array.

                您始终可以使用iterator_to_array 函数将您的对象转换为数组,但您无法逆转该过程.

                You can always turn your object into an array by using the iterator_to_array function, but you can't reverse that process.

                如果您认为需要反转可迭代对象中元素的顺序,那么您可以创建一个 reverse() 方法,该方法可能在内部使用 array_reverse().像这样:-

                If you see the need for reversing the order of the elements in your iterable object, then you could create a reverse() method that, possibly, uses array_reverse() internally. Something like this:-

                class Test implements Iterator
                {
                    private $testing = [0,1,2,3,4,5,6,7,8,9,10];
                    private $index = 0;
                
                    public function current()
                    {
                        return $this->testing[$this->index];
                    }
                
                    public function next()
                    {
                        $this->index ++;
                    }
                
                    public function key()
                    {
                        return $this->index;
                    }
                
                    public function valid()
                    {
                        return isset($this->testing[$this->key()]);
                    }
                
                    public function rewind()
                    {
                        $this->index = 0;
                    }
                
                    public function reverse()
                    {
                        $this->testing = array_reverse($this->testing);
                        $this->rewind();
                    }
                }
                
                $tests = new Test();
                var_dump(iterator_to_array($tests));
                $tests->reverse();
                var_dump(iterator_to_array($tests));
                

                输出:-

                array (size=11)
                  0 => int 0
                  1 => int 1
                  2 => int 2
                  3 => int 3
                  4 => int 4
                  5 => int 5
                  6 => int 6
                  7 => int 7
                  8 => int 8
                  9 => int 9
                  10 => int 10
                
                array (size=11)
                  0 => int 10
                  1 => int 9
                  2 => int 8
                  3 => int 7
                  4 => int 6
                  5 => int 5
                  6 => int 4
                  7 => int 3
                  8 => int 2
                  9 => int 1
                  10 => int 0
                

                我写了代码是为了在发布之前向自己证明它可以工作,并认为我不妨把它扔到答案中.

                I wrote the code to prove to myself that it would work before posting and thought I might as well throw it into the answer.

                这篇关于将实现 Iterator 的 PHP 类视为数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                <legend id='ZNhQ5'><style id='ZNhQ5'><dir id='ZNhQ5'><q id='ZNhQ5'></q></dir></style></legend>

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

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

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

                          <tbody id='ZNhQ5'></tbody>