問題描述
可能不是 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模板網!