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

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

      <tfoot id='amu5b'></tfoot>

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

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

        Eloquent 集合:each 與 foreach

        Eloquent collections: each vs foreach(Eloquent 集合:each 與 foreach)

          <bdo id='6cUyO'></bdo><ul id='6cUyO'></ul>

          <i id='6cUyO'><tr id='6cUyO'><dt id='6cUyO'><q id='6cUyO'><span id='6cUyO'><b id='6cUyO'><form id='6cUyO'><ins id='6cUyO'></ins><ul id='6cUyO'></ul><sub id='6cUyO'></sub></form><legend id='6cUyO'></legend><bdo id='6cUyO'><pre id='6cUyO'><center id='6cUyO'></center></pre></bdo></b><th id='6cUyO'></th></span></q></dt></tr></i><div class="qwawimqqmiuu" id='6cUyO'><tfoot id='6cUyO'></tfoot><dl id='6cUyO'><fieldset id='6cUyO'></fieldset></dl></div>
            <tbody id='6cUyO'></tbody>

                <legend id='6cUyO'><style id='6cUyO'><dir id='6cUyO'><q id='6cUyO'></q></dir></style></legend>
              • <tfoot id='6cUyO'></tfoot>
              • <small id='6cUyO'></small><noframes id='6cUyO'>

                • 本文介紹了Eloquent 集合:each 與 foreach的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

                  問題描述

                  可能不是 Eloquent 集合特有的問題,但在使用它們時它只是讓我感到震驚.讓我們假設我們有一個 $collection 對象,它是 IlluminateSupportCollection 的一個實例.

                  Might not be a question specific to Eloquent collections, but it just hit me while working with them. Let's just assume we have a $collection object which is an instance of IlluminateSupportCollection.

                  現(xiàn)在,如果我們想要迭代它,使用帶有閉包的 each() 與使用常規(guī)的 foreach 的優(yōu)缺點是什么.有嗎?

                  Now if we want to iterate over it, what are the pros and cons of using each() with a closure versus a regular foreach. Are there any?

                  foreach ($collection as $item) {
                      // Some code
                  }
                  

                  對比

                  $collection->each(function ($item) {
                      // Some code
                  });
                  

                  推薦答案

                  foreach 語句應該用作一種循環(huán)遍歷集合并對其執(zhí)行某種邏輯的方法.如果其中的內容影響程序中的其他內容,則使用此循環(huán).

                  A foreach statement should be used as a sort of a way to cycle through a collection and perform some sort of logic on it. If what is in it effects other things in the program, then use this loop.

                  .each 方法使用 array_map 循環(huán)遍歷集合中的每個對象并對每個對象執(zhí)行閉包.然后它返回結果數(shù)組.這就是關鍵!如果您想以某種方式更改集合,則應使用 .each.也許它是一系列汽車,而您想要使模型大寫或小寫.您只需將一個閉包傳遞給 .each 方法,該方法接受對象并在每個 Car 對象的模型上調用 strtoupper() .然后它返回帶有所做更改的集合.

                  The .each method uses array_map to cycle through each of the objects in the collection and perform a closure on each one. It then returns the resulting array. That is the key! .each should be used if you want to change the collection in some way. Maybe it's an array of cars and you want to make the model upper case or lower case. You would just pass a closure to the .each method that takes the object and calls strtoupper() on the model of each Car object. It then returns the collection with the changes that have been made.

                  故事的精神是這樣的:使用.each方法以某種方式改變數(shù)組中的每一項;使用 foreach 循環(huán)來使用每個對象來影響程序的其他部分(使用一些邏輯語句).

                  Morale of the story is this: use the .each method to change each item in the array in some way; use the foreach loop to use each object to affect some other part of the program (using some logic statement).

                  正如下面所說的那樣雄辯地(看看我在那里做了什么?),上面的答案有點偏離.使用 array_map.each 方法實際上從未使用過 array_map 調用的輸出.因此,由 array_map 創(chuàng)建的新數(shù)組不會保存在 Collection 中.要更改它,最好使用 .map 方法,該方法也存在于 Collection 對象中.

                  As stated so Eloquently (see what I did there?) below, the above answer is slightly off. The .each method using array_map never actually used the output from the array_map call. So, the new array created by array_map would not be saved on the Collection. To change it, you're better off using the .map method, which also exists on a Collection object.

                  使用 foreach 語句來迭代它們中的每一個更有意義,因為除非您確保使用 use<,否則您將無法訪問閉包之外的變量/code> 語句,這對我來說似乎很尷尬.

                  Using a foreach statement to iterate over each of them makes a bit more sense because you won't be able to access variables outside the Closure unless you make sure to use a use statement, which seems awkward to me.

                  上面的回答原來寫的時候的實現(xiàn)可以是在此處找到.

                  The implementation when the above answer was originally written can be found here.

                  他們在下面談論的新 .each 不再使用 array_map.它只是遍歷集合中的每個項目并調用傳入的 $callback,將項目及其在數(shù)組中的鍵傳遞給它.在功能上,它似乎工作相同.我相信在閱讀代碼時使用 foreach 循環(huán)會更有意義.但是,我看到了使用 .each 的好處,因為它允許您將方法鏈接在一起,如果您喜歡的話.如果您的業(yè)務邏輯要求您能夠這樣做,它還允許您從回調中返回 false 以提前離開循環(huán).

                  The new .each that they are talking about below no longer uses array_map. It simply iterates through each item in the collection and calls the passed in $callback, passing it the item and its key in the array. Functionally, it seems to work the same. I believe using a foreach loop would make more sense when reading the code. However, I see the benefits of using .each because it allows you to chain methods together if that tickles your fancy. It also allows you to return false from the callback to leave the loop early if your business logic demands you to be able to.

                  有關新實現(xiàn)的更多信息,請查看 源代碼.

                  For more info on the new implementation, check out the source code.

                  這篇關于Eloquent 集合:each 與 foreach的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!

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

                  相關文檔推薦

                  Deadlock exception code for PHP, MySQL PDOException?(PHP、MySQL PDOException 的死鎖異常代碼?)
                  PHP PDO MySQL scrollable cursor doesn#39;t work(PHP PDO MySQL 可滾動游標不起作用)
                  PHP PDO ODBC connection(PHP PDO ODBC 連接)
                  Using PDO::FETCH_CLASS with Magic Methods(使用 PDO::FETCH_CLASS 和魔術方法)
                  php pdo get only one value from mysql; value that equals to variable(php pdo 只從 mysql 獲取一個值;等于變量的值)
                  MSSQL PDO could not find driver(MSSQL PDO 找不到驅動程序)

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

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

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

                        <bdo id='NqkrQ'></bdo><ul id='NqkrQ'></ul>
                          <tbody id='NqkrQ'></tbody>
                            主站蜘蛛池模板: 久久久久亚洲 | 国产乱肥老妇国产一区二 | 精品一级 | 日本精品一区二区三区视频 | 国产精品乱码一区二区三区 | 日韩精品视频在线观看一区二区三区 | 亚洲综合色婷婷 | 国产黄色小视频在线观看 | 亚洲综合字幕 | 国产成人精品一区二区三区四区 | 亚洲国产欧美国产综合一区 | 欧一区二区| 久久久久国产一区二区 | 中文字幕乱码视频32 | 国产在线视频在线观看 | 欧美精品久久久久久久久久 | 国产在线观看一区二区三区 | 欧美日本韩国一区二区 | 成人免费观看男女羞羞视频 | 粉嫩av在线 | 青春草在线 | 久久久久久亚洲精品 | 精品av | 国产免费人成xvideos视频 | 国产精品久久久久久久久久久久 | 黑人巨大精品 | 国产欧美日韩精品在线观看 | 91豆花视频| 日韩影音 | 久久久久国产精品午夜一区 | 亚洲精品乱码久久久久久蜜桃91 | 国产日产欧产精品精品推荐蛮挑 | 成人性生交a做片 | 成人av一区二区亚洲精 | 精品久久久久久久人人人人传媒 | 欧美日韩在线观看一区 | 久久久999成人 | 日韩免费福利视频 | 亚洲成人一区二区 | 欧美11一13sex性hd | 国产成人精品一区二区三区四区 |