PHP Array Filter by Key
How To Filter Arrays In PHP By Key
Assuming we have an array with keys we want to retrieve from $_REQUEST
$allowed = array('mac', 'name', 'description');
What would be the best way to filter $_REQUEST
based on $allowed
keys?
The simplest would be to use:
$out = array_intersect_key($_REQEST, array_flip($allowed));
Other implementations
One solution would be to loop through the keys array and save the values of the source array in an output array. Sounds complicated already
$out = array();
foreach($allowed as $key)
{
if(!array_key_exists($key, $_REQEST)) continue;
$out[$key] = $_REQUEST[$key];
}
print_r($out) //Array ( [a] => 1 [d] => 4 )
If we wanted to use a closure, and one of the array methods, we could try with array_fiter
:
$out = array();
$filter = function($key) use($_REQUEST, &$out){
if(array_key_exists($key, $_REQUEST))
$out[$key] = $_REQUEST[$key];
};
array_walk($allowed, $filter);
print_r($out) //Array ( [a] => 1 [d] => 4 )