Skip to content

Instantly share code, notes, and snippets.

@AkenRoberts
Last active August 29, 2015 14:26
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 AkenRoberts/4c5a45fce2f3a148487b to your computer and use it in GitHub Desktop.
Save AkenRoberts/4c5a45fce2f3a148487b to your computer and use it in GitHub Desktop.
<?php
$collection = [$obj1, $obj2, $obj3];
// Dumb way
$matches = array_reduce($collection, function ($matches, $object) {
if ($object->isMatch()) {
$matches[] = $object;
}
return $matches;
}, []);
// Functionally equivalent, faster, easier to read
$matches = [];
foreach ($collection as $object) {
if ($object->isMatch()) {
$matches[] = $object;
}
}
// Fancy Collection-object way
$matches = $collection->filter(function ($object) {
return $object->isMatch();
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment