Last active
August 29, 2015 14:26
-
-
Save AkenRoberts/4c5a45fce2f3a148487b to your computer and use it in GitHub Desktop.
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 | |
$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