mardi 14 février 2017

Laravel 5.* - Collections - Recursive merge / Merge in depth

Is it possible to recursively merge two eloquent collections in Laravel 5.* when they themselves have child collections?

For example if i had two collections like this:

$collection1 = collect([
   'sub1' => collect(['foo','bar']),
   'sub2' => collect(['umm','what'])
]);

$collection2 = collect([
   'sub1' => collect(['hello','world']),
   'sub2' => collect(['nice','day'])
]);

And then i were to then to do something similar to the merge function:

$collection3 = $collection1->merge($collection2, 'recursive');

Is it possible to get an output the equivalent to this?

$collection3 = collect([
   'sub1' => collect(['hello','world','foo','bar']),
   'sub2' => collect(['nice','day','umm','what'])
]);

I can write a custom function to do this but i am interested to see if Laravel has something out of the box to handle it!



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire