Filter array by its keys using an array of allowed keys
The callback function in array_filter()
only passes in the array's values, not the keys.
If I have:
$my_array = array("foo" => 1, "hello" => "world");
$allowed = array("foo", "bar");
What's the best way to delete all keys in $my_array
that are not in the $allowed
array?
Desired output:
$my_array = array("foo" => 1);
With array_intersect_key
and array_flip
:
var_dump(array_intersect_key($my_array, array_flip($allowed)));
array(1) {
["foo"]=>
int(1)
}
PHP 5.6 introduced a third parameter to array_filter()
, flag
, that you can set to ARRAY_FILTER_USE_KEY
to filter by key instead of value:
$my_array = ['foo' => 1, 'hello' => 'world'];
$allowed = ['foo', 'bar'];
$filtered = array_filter(
$my_array,
function ($key) use ($allowed) {
return in_array($key, $allowed);
},
ARRAY_FILTER_USE_KEY
);
Since PHP 7.4 introduced arrow functions we can make this more succinct:
$my_array = ['foo' => 1, 'hello' => 'world'];
$allowed = ['foo', 'bar'];
$filtered = array_filter(
$my_array,
fn ($key) => in_array($key, $allowed),
ARRAY_FILTER_USE_KEY
);
Clearly this isn't as elegant as array_intersect_key($my_array, array_flip($allowed))
, but it does offer the additional flexibility of performing an arbitrary test against the key, e.g. $allowed
could contain regex patterns instead of plain strings.
You can also use ARRAY_FILTER_USE_BOTH
to have both the value and the key passed to your filter function. Here's a contrived example based upon the first, but note that I'd not recommend encoding filtering rules using $allowed
this way:
$my_array = ['foo' => 1, 'bar' => 'baz', 'hello' => 'wld'];
$allowed = ['foo' => true, 'bar' => true, 'hello' => 'world'];
$filtered = array_filter(
$my_array,
// N.b. it's ($val, $key) not ($key, $val):
fn ($val, $key) => isset($allowed[$key]) && (
$allowed[$key] === true || $allowed[$key] === $val
),
ARRAY_FILTER_USE_BOTH
); // ['foo' => 1, 'bar' => 'baz']
I needed to do same, but with a more complex array_filter
on the keys.
Here's how I did it, using a similar method.
// Filter out array elements with keys shorter than 4 characters
$a = array(
0 => "val 0",
"one" => "val one",
"two" => "val two",
"three"=> "val three",
"four" => "val four",
"five" => "val five",
"6" => "val 6"
);
$f = array_filter(array_keys($a), function ($k){ return strlen($k)>=4; });
$b = array_intersect_key($a, array_flip($f));
print_r($b);
This outputs the result:
Array
(
[three] => val three
[four] => val four
[five] => val five
)