With array_intersect_key and array_flip:

var_dump(array_intersect_key($my_array, array_flip($allowed)));

array(1) {
  ["foo"]=>
  int(1)
}
Answer from Vincent Savard on Stack Overflow
🌐
PHP
php.net › manual › en › function.array-filter.php
PHP: array_filter - Manual
Array keys are preserved, and may result in gaps if the array was indexed. The result array can be reindexed using the array_values() function. ... If no callback is supplied, all empty entries of array will be removed. See empty() for how PHP defines empty in this case. ... ARRAY_FILTER_USE_BOTH - pass both value and key as arguments to callback instead of the value Default is 0 which will pass value as the only argument to callback instead.
Top answer
1 of 12
539

With array_intersect_key and array_flip:

var_dump(array_intersect_key($my_array, array_flip($allowed)));

array(1) {
  ["foo"]=>
  int(1)
}
2 of 12
486

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 (allowed) {
        // N.b. in_array() is notorious for being slow 
        return in_array(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(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,
    fn (key) => isset($allowed[$key]) && (
        $allowed[$key] === true || $allowed[val
    ),
    ARRAY_FILTER_USE_BOTH
); // ['foo' => 1, 'bar' => 'baz']
🌐
GitHub
yellowduck.be › posts › filtering-an-array-by-keys-in-php
🐥 Filtering an array by keys in PHP
ARRAY_FILTER_USE_BOTH: Passing ARRAY_FILTER_USE_BOTH allows the closure to access both the key and value of each item in the array. ... If you just need to filter by keys: Go with array_intersect_key.
🌐
Pine
pineco.de › snippets › filtering-arrays-by-keys
Filtering Arrays by Keys - Pine
January 31, 2024 - $set = ['a', 'b', 'c', 'd']; // Default array_filter($set, function ($item) { return $item > 'b'; }); // Using keys array_filter($set, function ($key), { return $key > 1; }, ARRAY_FILTER_USE_KEY); // Using both array_filter($set, function ($item, ...
🌐
vimtutor
remarkablemark.org › blog › 2020 › 09 › 09 › php-filter-array-by-keys
PHP filter array by keys | remarkablemark
September 9, 2020 - function filterArrayByKeys(array $array, array $keys): array { return array_intersect_key($array, array_flip($keys)); }
🌐
W3Schools
w3schools.com › php › func_array_filter.asp
PHP array_filter() Function
PHP Examples PHP Compiler PHP Quiz PHP Exercises PHP Server PHP Syllabus PHP Study Plan PHP Certificate ... array() array_change_key_case() array_chunk() array_column() array_combine() array_count_values() array_diff() array_diff_assoc() array_diff_key() array_diff_uassoc() array_diff_ukey() array_fill() array_fill_keys() array_filter() array_flip() array_intersect() array_intersect_assoc() array_intersect_key() array_intersect_uassoc() array_intersect_ukey() array_key_exists() array_keys() array_map() array_merge() array_merge_recursive() array_multisort() array_pad() array_pop() array_produc
🌐
CodexWorld
codexworld.com › home › how to guides › how to filter multidimensional array by key value in php
How to Filter Multidimensional Array by Key Value in PHP - CodexWorld
January 4, 2023 - PHP array_filter() function filters multidimensional array by key value. Filter values from multidimensional array similar to SQL LIKE using array_filter() function in PHP.
Find elsewhere
🌐
Honar Systems
honarsystems.com › home › php array filter function
PHP Array Filter Function
August 3, 2025 - The PHP array_filter() function is used to filter elements from an array by key or value, or a custom user-defined callback function.
🌐
Reintech
reintech.io › blog › mastering-php-array-filter-function-for-filtering-arrays
Mastering PHP's `array_filter()` Function for Filtering Arrays | Reintech media
April 14, 2023 - The ARRAY_FILTER_USE_KEY constant in PHP is used as a flag with the array_filter() function. By default, array_filter() filters an array by its values.
🌐
FlatCoding
flatcoding.com › home › php array_filter: how to filter array values with examples
PHP array_filter: How to Filter Array Values with Examples - FlatCoding
August 15, 2025 - The array_filter works with associative arrays just like with indexed arrays. You can also pass the ARRAY_FILTER_USE_KEY flag to filter by keys.
🌐
Benjamin Crozat
benjamincrozat.com › home › blog › php › understanding array_filter() in php
Understanding array_filter() in PHP
November 11, 2023 - To finish this up, another common mistake is forgetting that array keys are preserved. This might lead to unexpected gaps in the numeric array indexes: $array = [1, 2, 3, 4, 5]; $even = array_filter($array, fn ($value) => $value % 2 == 0); // Array // ( // [1] => 2 // [3] => 4 // ) print_r($even); ... Help me reach more people by sharing this article on social media! ... Tinkerwell lets you code and debug your PHP, Laravel, Symfony, WordPress, etc., apps in an editor designed for fast feedback and quick iterations.
🌐
Designcise
designcise.com › web › tutorial › how-to-use-the-php-array-filter-function-to-filter-by-keys
Use PHP array_filter() to Filter by Keys - Designcise
August 6, 2021 - To use the PHP array_filter() function to filter array elements by key instead of value, you can pass the ARRAY_FILTER_USE_KEY flag as the third argument to the function. This would pass the key as the only argument to the provided callback function.
🌐
Amir Kamizi
amirkamizi.com › blog › php-array-filter
PHP array filter | Amir Kamizi
February 14, 2023 - $array = [ "twitter" => "Pratham", "Feedhive" => "Simon", "PHP" => "Amir", "Saas" => "Simon", "CSS" => "Pratham" ]; We want to filter the ones that have Pratham as the value · $filtered = array_filter($array, function ($value) { return $value == 'Pratham'; }); print_r($filtered); // Array ( [twitter] => Pratham [CSS] => Pratham ) Now let’s filter based on the key and filter the ones that have Feedhive as the key
🌐
Reddit
reddit.com › r/phphelp › why does array_filter in php use the original indices?
r/PHPhelp on Reddit: Why does array_filter in PHP use the original indices?
September 4, 2022 -

If I have an array of things, and I filter them so I end up with an array of a single thing from the original things, the index of that thing is what it was in the original array. Why is that? That seems like odd behavior.

Eg. the below code only works if the item I'm filtering for was the 0th item in arrays. I know I can use array_values() to reset the indices of the filtered array, but I'm just wondering why this is a thing

$items = [
	'stick',
	'ball',
	'bat',
	'computer'
];

$filtered = array_filter($items, function($item) {
	return $item == 'bat';
});
var_dump($filtered[0]); // I need to use the original index from $items in order for this to work; can't always use 0
🌐
PHP Tutorial
phptutorial.net › home › php tutorial › php array_filter function
PHP array_filter Function
April 6, 2025 - To pass both the key and value of the element to the callback function, you pass the ARRAY_FILTER_USE_BOTH value as the third argument of the array_filter() function. For example: <?php $inputs = [ 'first' => 'John', 'last' => 'Doe', 'password' => 'secret', 'email' => '' ]; $filtered = ...
🌐
PHP Freaks
forums.phpfreaks.com › php coding › php coding help
Filter array by KEY - PHP Coding Help - PHP Freaks
May 16, 2018 - I would like to create an array from the one below that can sort of filter using date ranges; For example, make an array that contains data using date -1.174 ...
🌐
Scaler
scaler.com › home › topics › php array_filter() function
PHP array_filter() Function - Scaler Topics
April 1, 2024 - ARRAY_FILTER_USE_BOTH: This callback function receives both the key and value of the array as arguments. The array_filter() method in PHP creates a new array that contains just the elements for which the callback function returns true.