Filter an array in PHP
This article demonstrates how to filter an array in PHP. You’re given a list of permissible keys, and the solution should only retain the keys present in the list.
1. Using array_intersect_key() function
The array_intersect_key() function computes the intersection of arrays using keys for comparison. The array_intersect_key() function takes the input array to filter, and another array to compare keys against.
The following code demonstrates the usage of array_intersect_key() to filter an array. It is worth noting that the array containing the permissible keys is flipped using the array_flip() function before being passed to the array_intersect_key() function, so that its values become keys.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
<?php $array = array('key1' => 'value1', 'key2' => 'value2', 'key3' => 'value3'); $allowed_keys = array('key1', 'key4'); $filtered_array = array_intersect_key($array, array_flip($allowed_keys)); print_r($filtered_array); /* Output: Array ( [key1] => value1 ) */ ?> |
If you need to apply a more complex filter to the keys, you can do so using the array_filter() function. For example,
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
<?php $array = array(0 => 'blue', 1 => 'red', 2 => 'green', 3 => 'red'); $even_keys = array_filter(array_keys($array), function ($value) { return is_numeric($value) && $value % 2 == 0; }); $filtered_array = array_intersect_key($array, array_flip($even_keys)); print_r($filtered_array); /* Output: Array ( [0] => blue [2] => green ) */ ?> |
2. Using array_filter() function
Alternatively, you can directly invoke the array_filter() function to filter elements of an array using a callback function.
The default argument to the callback function is the array’s values. You can even specify which arguments are sent to the callback. If flag ARRAY_FILTER_USE_KEY is passed as a third parameter, the key is passed as the argument to the callback instead of the value.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
<?php $array = array('key1' => 'value1', 'key2' => 'value2', 'key3' => 'value3'); $allowed_keys = array('key1', 'key4'); $filtered_array = array_filter($array, function ($key) use ($allowed_keys) { return in_array($key, $allowed_keys); }, ARRAY_FILTER_USE_KEY); print_r($filtered_array); /* Output: Array ( [key1] => value1 ) */ ?> |
Alternatively, you can pass ARRAY_FILTER_USE_BOTH flag to have both value and key passed as arguments to the callback function. This flag can be used to filter an array based on both keys and values, as shown below:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
<?php $array = array('key1' => 'value1', 'key2' => 'value2', 'key3' => 'value3'); $allowed = array('key1' => 'value1', 'key2' => 'value_2'); $filtered_array = array_filter($array, function ($value, $key) use ($allowed) { return isset($allowed[$key]) && $allowed[$key] === $value; }, ARRAY_FILTER_USE_BOTH); print_r($filtered_array); /* Output: Array ( [key1] => value1 ) */ ?> |
That’s all about filtering an array in PHP.
Thanks for reading.
To share your code in the comments, please use our online compiler that supports C, C++, Java, Python, JavaScript, C#, PHP, and many more popular programming languages.
Like us? Refer us to your friends and support our growth. Happy coding :)