Skip to content

Instantly share code, notes, and snippets.

@h4cc
Created October 6, 2014 12:29
Show Gist options
  • Star 12 You must be signed in to star a gist
  • Fork 6 You must be signed in to fork a gist
  • Save h4cc/8e2e3d0f6a8cd9cacde8 to your computer and use it in GitHub Desktop.
Save h4cc/8e2e3d0f6a8cd9cacde8 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));
}
@Sky161
Copy link

Sky161 commented May 19, 2015

This is work on php 5.3?

@jakwuh
Copy link

jakwuh commented Aug 19, 2015

@Sky161 Yes, is it

@well1791
Copy link

Nice!!, thanks man!

@Bob0t
Copy link

Bob0t commented Apr 5, 2016

Ah so cool man ! thx !

@Tinram
Copy link

Tinram commented Sep 9, 2016

Excellent function. Thank you.

@zvaehn
Copy link

zvaehn commented Sep 12, 2016

thank you so much for this little snippet!

@calexandrepcjr
Copy link

THANKS MAN, WORKED!!!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment