Movatterモバイル変換


[0]ホーム

URL:


Skip to content
DEV Community
Log in Create account

DEV Community

Jacob Landry
Jacob Landry

Posted on

Illuminate Collections vs. PHP Arrays

I recently published a deep-dive look at the difference between Laravel Collections and standard PHP functionality on Hackernoon.

Have a look!
https://hackernoon.com/illuminate-collections-vs-php-arrays

Top comments(2)

Subscribe
pic
Create template

Templates let you quickly answer FAQs or store snippets for re-use.

Dismiss
CollapseExpand
 
xwero profile image
david duymelinck
Learned to code in the wild west time of php 4, also the time xml and xpath where the new hot thing.
  • Location
    Belgium
  • Joined

The biggest difference between Laravel collections and php is that you canadd your own methods. I think that is more important than a fluent api.

The code of the examples doesn't show how a collection is more readable. Even the all together example looks very readable. The only thing I would change are the variable names to make it clear what the flow is.

$b=array_filter($a,function($row){returnsubstr($row,0,1)==="a";});$c=array_search(function($row){returnsubstr($row,0,1)==="a";},$b);$d=array_map(function($row){return"test";},$c);sort($d);foreach($das$item){$doSomething=true;}$sum=array_reduce($d,function($carry,$row){return$carry+strlen($row);});
Enter fullscreen modeExit fullscreen mode

Naming things, even just with a letter, is very powerful. In this case to establish a flow. When there is a letter out of place you will notice it.

If a collection/array requires a lot of actions, why not check if the actions can be reduced before it gets to that part of your code, for example with a data object.

In short I think most of the time there is a better solution than relying on a Collection instance.

CollapseExpand
 
suckup_de profile image
Lars Moelleken
"There is nothing good unless you do it." - Erich Kästner

I builded many collection classes and often the simplest way is just a array with phpdocs. 😜chatgpt.com/share/671d8a50-aa5c-80...

Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment'spermalink.

For further actions, you may consider blocking this person and/orreporting abuse

Dev...writer...gamer...
  • Location
    Winthrop, Maine
  • Joined

More fromJacob Landry

DEV Community

We're a place where coders share, stay up-to-date and grow their careers.

Log in Create account

[8]ページ先頭

©2009-2025 Movatter.jp