Skip to content

Instantly share code, notes, and snippets.

@muhamed-didovic
Forked from h4cc/array_filter_key.php
Last active August 29, 2015 14:11
Show Gist options
  • Save muhamed-didovic/5c5d468eab039170207d to your computer and use it in GitHub Desktop.
Save muhamed-didovic/5c5d468eab039170207d to your computer and use it in GitHub Desktop.
<?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