-
-
Save Rudde/b12fd574a3f548b847ffb9b4e66baf89 to your computer and use it in GitHub Desktop.
Filtering a PHP array by key instead of value.
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 | |
/** | |
* Filtering a array by its keys using a callback. | |
* | |
* @param $array array The array to filter | |
* @param $callback Callback The filter callback, that will get the key as first argument. | |
* | |
* @return array The remaining key => value combinations from $array. | |
*/ | |
function array_filter_key(array $array, $callback) | |
{ | |
$matchedKeys = array_filter(array_keys($array), $callback); | |
return array_intersect_key($array, array_flip($matchedKeys)); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment