Array in Laravel, source code locationIlluminateSupportArr.php;
Collections in Laravel, source code locationIlluminateSupportCollection.php;
A collection is a re-encapsulation of an array and is presented in the form of an object; it provides many method functions (most of these methods use callback functions internally), which is much more flexible than array-based operations;
Essentially, it’s the difference between operating elements in object-oriented form and operating elements in array form; if you operate with objects, as vika_倾慕 said, you can chain operations; if you operate with arrays, many intermediate temporary variables or statements and codes will be generated Seems verbose;
What makes collections more powerful than arrays is that they use various map/reduce intuitive operations. For example, we remove all inactive user models and collect the names of the remaining users:
Array in Laravel, source code location
IlluminateSupportArr.php
;Collections in Laravel, source code location
IlluminateSupportCollection.php
;A collection is a re-encapsulation of an array and is presented in the form of an object; it provides many method functions (most of these methods use callback functions internally), which is much more flexible than array-based operations;
Essentially, it’s the difference between operating elements in object-oriented form and operating elements in array form; if you operate with objects, as
vika_倾慕
said, you can chain operations; if you operate with arrays, many intermediate temporary variables or statements and codes will be generated Seems verbose;What makes collections more powerful than arrays is that they use various map/reduce intuitive operations. For example, we remove all inactive user models and collect the names of the remaining users:
If you think it’s okay, remember to adopt it