Skip to content

Instantly share code, notes, and snippets.

@ChrisBuchholz
Last active December 21, 2015 00:59
Show Gist options
  • Save ChrisBuchholz/6224198 to your computer and use it in GitHub Desktop.
Save ChrisBuchholz/6224198 to your computer and use it in GitHub Desktop.
<?php
function pluckWith($key, $array) {
return function($value) use ($key, $array) {
$matches = [];
foreach ($array as $k => $v)
if (array_key_exists($key, $v) && $v[$key] == $value)
array_push($matches, $array[$k]);
return $matches;
};
}
$db = [
['name' => 'carlos'],
['name' => 'tihi']];
$getByName = pluckWith('name', $db);
$peopleNamedCarlos = $getByName('carlos');
print_r($peopleNamedCarlos);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment