5 Ways – Get Array Values by Matched or Excluded Keys Set

In this PHP tutorial, I’m providing 5 functions to get array values by keys. We have an associative array and an indexed array of few keys. Further, we want to get an array that has matched keys from the indexed array and corresponding values from the associative array.

Conversely, we might wish to return an array with certain keys and values excluded from the associative arrays. The first three code snippets are ways to get an array of matching keys. Whereas the last two remove given keys/values and return the rest. Also, we’re using the following arrays to understand all snippets given here.

Get Array Values by Matched or Excluded Keys Set

Here are more excellent tutorials about Arrays in PHP at the Fellow Tuts blog:

1. Foreach Loop to Get Array Values by Set of Keys

This is the traditional method that most of the developers use. While this gives the desired output, it’s not the preferred method. Since we can code better so let us see alternate functions to get the array.

However, we have used isset() instead of array_key_exists() function for PHP performance benefit. You are free to use either to check if the key exists in the array.

2. An Array of Matching Keys Using  array_intersect_key()

array_intersect_key() in conjunction with array_flip() drastically reduces the code required. The array_flip() function flips keys and values. So keys turn to values and values become keys. here, it converts the indexed array into an associative array.

Further, array_intersect_key() returns an array containing all entries from the first array having keys given in the second array. Additionally, remember that this function preserves keys order of the first parameter which is different from the previous implementation.

Again, if you want to get rid of the array_flip() function then pass the second argument as an associative array, as shown below:

3. Get Array Values by Keys with array_map()

Use this function for directly getting array values with the set of matching keys. Also, note that this implementation doesn’t need multiple PHP array functions to get the result. Additionally, it returns an indexed/numeric array so no need to call array_values() function.

However, keep in mind that the keys array shouldn’t contain any key that is absent in the associative array. Otherwise, you will face “Undefined index” notice by PHP.

4. Get Array Subset Excluding Certain Keys

Till yet, we have obtained an array containing given and matching keys. Instead, we might need an array which doesn’t include given keys. Here is the first method to do so. The long way is, loop through keys array and unset that entry from the associative array.

5. Get Array Values with Keys Excluded

The last method is just a slight change in the second example of “array_intersect_key()” here. Instead of array_intersect_key(), use array_diff_key() to filter out elements with key names to exclude. So our function would be:

So these are the ways that can filter an associative array based on certain keys. You can get not only matched elements but also the opposite too. Also, you can find an extensive list of array intersection and difference functions in official PHP documentation.

Do you know any other way to get array values by matched or excluded keys set? We would like to add them here. Just let us know in the comment form. Also, write as well if you wish to fix any trouble related to arrays in PHP.

You Might Interested In

Leave a Reply

Enclose a code block like: <pre><code>Your Code Snippet</code></pre>.