Created
August 25, 2023 08:38
-
-
Save andyg1/84b964d81aeeb9c418890fdeb20fb855 to your computer and use it in GitHub Desktop.
Laravel - convert an array including its children to Collections
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// source: https://laravel-code.tips/add-a-recursive-macro-to-convert-an-array-including-its-children-to-collections/ | |
// Pop this in boot() of a service provider | |
Collection::macro('recursive', function () { | |
return $this->map(function ($value) { | |
if (is_array($value) || is_object($value)) { | |
return collect($value)->recursive(); | |
} | |
return $value; | |
}); | |
// Usage | |
collect($array)->recursive(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment