pbootcms网站模板|日韩1区2区|织梦模板||网站源码|日韩1区2区|jquery建站特效-html5模板网

  • <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="95nrdhj" 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="x7xlhpd" 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.

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

                  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 語句應該用作一種循環遍歷集合并對其執行某種邏輯的方法.如果其中的內容影響程序中的其他內容,則使用此循環.

                  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 循環遍歷集合中的每個對象并對每個對象執行閉包.然后它返回結果數組.這就是關鍵!如果您想以某種方式更改集合,則應使用 .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方法以某種方式改變數組中的每一項;使用 foreach 循環來使用每個對象來影響程序的其他部分(使用一些邏輯語句).

                  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 創建的新數組不會保存在 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.

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

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

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

                  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.

                  有關新實現的更多信息,請查看 源代碼.

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

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

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

                  相關文檔推薦

                  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="fpd5nhn" 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>
                            主站蜘蛛池模板: 昆山新莱洁净应用材料股份有限公司-卫生级蝶阀,无菌取样阀,不锈钢隔膜阀,换向阀,离心泵 | 窖井盖锯圆机_锯圆机金刚石锯片-无锡茂达金刚石有限公司 | 淘剧影院_海量最新电视剧,免费高清电影随心观看 | 并离网逆变器_高频UPS电源定制_户用储能光伏逆变器厂家-深圳市索克新能源 | 伸缩器_伸缩接头_传力接头-巩义市润达管道设备制造有限公司 | 沈阳建筑设计公司_加固改造设计_厂房设计_设计资质加盟【金辉设计】 | 北京中创汇安科贸有限公司 | 培训一点通 - 合肥驾校 - 合肥新亚驾校 - 合肥八一驾校 | 云南外加剂,云南速凝剂,云南外加剂代加工-普洱澜湄新材料科技有限公司 | 不干胶标签-不干胶贴纸-不干胶标签定制-不干胶标签印刷厂-弗雷曼纸业(苏州)有限公司 | SRRC认证|CCC认证|CTA申请_IMEI|MAC地址注册-英利检测 | 【星耀裂变】_企微SCRM_任务宝_视频号分销裂变_企业微信裂变增长_私域流量_裂变营销 | 车件|铜件|车削件|车床加工|五金冲压件-PIN针,精密车件定制专业厂商【东莞品晔】 | 哲力实业_专注汽车涂料汽车漆研发生产_汽车漆|修补油漆品牌厂家 长沙一级消防工程公司_智能化弱电_机电安装_亮化工程专业施工承包_湖南公共安全工程有限公司 | 合肥白癜风医院_[治疗白癜风]哪家好_合肥北大白癜风医院 | 别墅图纸超市|别墅设计图纸|农村房屋设计图|农村自建房|别墅设计图纸及效果图大全 | 小小作文网_中小学优秀作文范文大全 | 昆山新莱洁净应用材料股份有限公司-卫生级蝶阀,无菌取样阀,不锈钢隔膜阀,换向阀,离心泵 | 专业的新乡振动筛厂家-振动筛品质保障-环保振动筛价格—新乡市德科筛分机械有限公司 | 仿古建筑设计-仿古建筑施工-仿古建筑公司-汉匠古建筑设计院 | TPE塑胶原料-PPA|杜邦pom工程塑料、PPSU|PCTG材料、PC/PBT价格-悦诚塑胶 | 宝宝药浴-产后药浴-药浴加盟-艾裕-专注母婴调养泡浴 | 铝板冲孔网,不锈钢冲孔网,圆孔冲孔网板,鳄鱼嘴-鱼眼防滑板,盾构走道板-江拓数控冲孔网厂-河北江拓丝网有限公司 | 高考志愿规划师_高考规划师_高考培训师_高报师_升学规划师_高考志愿规划师培训认证机构「向阳生涯」 | 环境模拟实验室_液体-气体控温机_气体控温箱_无锡双润冷却科技有限公司 | 会议会展活动拍摄_年会庆典演出跟拍_摄影摄像直播-艾木传媒 | 东莞动力锂电池保护板_BMS智能软件保护板_锂电池主动均衡保护板-东莞市倡芯电子科技有限公司 | 山东钢衬塑罐_管道_反应釜厂家-淄博富邦滚塑防腐设备科技有限公司 | 台式低速离心机-脱泡离心机-菌种摇床-常州市万丰仪器制造有限公司 | 内窥镜-工业内窥镜厂家【上海修远仪器仪表有限公司】 | 碳钢法兰厂家,非标法兰,定制异型,法兰生产厂家-河北九瑞管道 | 微信小程序定制,广州app公众号商城网站开发公司-广东锋火 | 湖州织里童装_女童男童中大童装_款式多尺码全_织里儿童网【官网】-嘉兴嘉乐网络科技有限公司 | 超声波破碎仪-均质乳化机(供应杭州,上海,北京,广州,深圳,成都等地)-上海沪析实业有限公司 | 无缝钢管-聊城无缝钢管-小口径无缝钢管-大口径无缝钢管 - 聊城宽达钢管有限公司 | 阿里巴巴诚信通温州、台州、宁波、嘉兴授权渠道商-浙江联欣科技提供阿里会员办理 | 翅片管散热器价格_钢制暖气片报价_钢制板式散热器厂家「河北冀春暖气片有限公司」 | 东莞市海宝机械有限公司-不锈钢分选机-硅胶橡胶-生活垃圾-涡电流-静电-金属-矿石分选机 | 多物理场仿真软件_电磁仿真软件_EDA多物理场仿真软件 - 裕兴木兰 | 澳洁干洗店加盟-洗衣店干洗连锁「澳洁干洗免费一对一贴心服务」 干洗加盟网-洗衣店品牌排行-干洗设备价格-干洗连锁加盟指南 | 骨灰存放架|骨灰盒寄存架|骨灰架厂家|智慧殡葬|公墓陵园管理系统|网上祭奠|告别厅智能化-厦门慈愿科技 |