Skip to content

Instantly share code, notes, and snippets.

@Rudde
Forked from h4cc/array_filter_key.php
Created May 14, 2016 00:27
Show Gist options
  • Save Rudde/b12fd574a3f548b847ffb9b4e66baf89 to your computer and use it in GitHub Desktop.
Save Rudde/b12fd574a3f548b847ffb9b4e66baf89 to your computer and use it in GitHub Desktop.
Filtering a PHP array by key instead of value.
<?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