Skip to content

Instantly share code, notes, and snippets.

@skollro
Last active April 29, 2017 21:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save skollro/ac5c7eacc92c66c5c48f3179bb1be10a to your computer and use it in GitHub Desktop.
Save skollro/ac5c7eacc92c66c5c48f3179bb1be10a to your computer and use it in GitHub Desktop.
A join collection macro
<?php
Collection::macro('join', function ($items, callable $callback) {
return $this->flatMap(function ($value) use ($items, $callback) {
return $items->filter(function ($item) use ($value, $callback) {
return $callback($value, $item);
})->map(function ($item) use ($value) {
return new Collection([$value, $item]);
});
});
});
$first = collect([0, 1, 2]);
$second = collect([0, 0, 1]);
$result = $first->join($second, function ($a, $b) {
return $a == $b;
});
dd($result);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment