久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

  1. <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 class="qwawimqqmiuu" 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>
    1. <tfoot id='iVSJc'></tfoot>

      將實現 Iterator 的 PHP 類視為數組

      Treat a PHP class that implements Iterator as an array(將實現 Iterator 的 PHP 類視為數組)

        <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 class="qwawimqqmiuu" id='HFkcF'><tfoot id='HFkcF'></tfoot><dl id='HFkcF'><fieldset id='HFkcF'></fieldset></dl></div>

            <tbody id='HFkcF'></tbody>
          <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'>

              • <tfoot id='HFkcF'></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模板網!

                【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!

                相關文檔推薦

                MySQLi prepared statement amp; foreach loop(MySQLi準備好的語句amp;foreach 循環)
                Is mysqli_insert_id() gets record from whole server or from same user?(mysqli_insert_id() 是從整個服務器還是從同一用戶獲取記錄?)
                PHP MySQLi doesn#39;t recognize login info(PHP MySQLi 無法識別登錄信息)
                mysqli_select_db() expects exactly 2 parameters(mysqli_select_db() 需要 2 個參數)
                Php mysql pdo query: fill up variable with query result(Php mysql pdo 查詢:用查詢結果填充變量)
                MySQLI 28000/1045 Access denied for user #39;root#39;@#39;localhost#39;(MySQLI 28000/1045 用戶“root@“localhost的訪問被拒絕)
                <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 class="qwawimqqmiuu" 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>
                        1. 主站蜘蛛池模板: 99这里只有精品视频 | 亚洲三级国产 | 四虎最新视频 | 亚洲视频二区 | 免费精品 | 午夜精品一区 | 日韩中文字幕高清 | 91色视频在线观看 | h片在线观看网站 | 成人欧美日韩一区二区三区 | 999免费视频| 久久久综合网 | 午夜在线电影网 | 欧美不卡在线 | 免费观看色| 一区在线免费视频 | 日韩一区二区三区在线观看 | 日韩at| 国产99久久精品 | 国产精品欧美精品日韩精品 | 91精品久久久久久久久久小网站 | 免费视频二区 | 日本激情一区二区 | 色婷婷综合久久久中字幕精品久久 | 999久久久| 欧美一区二区 | 欧美日韩精品亚洲 | 免费高潮视频95在线观看网站 | 天天看天天干 | 国产日韩在线观看一区 | 毛片在线看片 | 在线不卡av| 精品欧美一区二区三区久久久 | 成人免费精品视频 | 亚洲成人网在线播放 | 国产乱码精品一区二区三区中文 | 欧美一区在线视频 | 国产一区二区三区精品久久久 | 国产在线精品一区二区 | 日本爱爱视频 | 亚洲视频在线免费观看 |