Created
June 19, 2013 19:47
-
-
Save dhrrgn/5817420 to your computer and use it in GitHub Desktop.
preg_grep_keys() allows you to grep over the keys of an array.
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 | |
function preg_grep_keys($pattern, $input) { | |
return preg_grep($pattern, array_keys($input)); | |
} |
of course it did
This is the best one.
Better version available in the comments at http://php.net/manual/en/function.preg-grep.php (includes flags, this doesn't support inverse).
function preg_grep_keys( $pattern, $input, $flags = 0 )
{
$keys = preg_grep( $pattern, array_keys( $input ), $flags );
$vals = array();
foreach ( $keys as $key )
{
$vals[$key] = $input[$key];
}
return $vals;
}
10/10 confirmed best function; using for production scale application
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Did this really need a function though?