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.
1 2 3 4 | $colors = array('white' => 'ffffff', 'black' => '000000', 'red' => 'ff0000', 'green'=> '00ff00', 'blue' => '0000ff'); $rgb = array('green', 'blue', 'red', 'rainbow'); $bnw = array('black' => '0,0,0', 'white' => '255,255,255'); |
Here are more excellent tutorials about Arrays in PHP at the Fellow Tuts blog:
- Change array key without changing the order
- Find object by values in an array of object
- Deleting an element by its value from an array
1. Foreach Loop to Get Array Values by Set of Keys
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 | function colors_using_loop($clrs, $keys){ $arry = array(); foreach($keys as $k){ //if(array_key_exists($k, $clrs)){ if(isset($clrs[$k])){ $arry[$k] = $clrs[$k]; } } return $arry; } $arr = colors_using_loop($colors, $rgb); print_r($arr); /* Array ( [green] => 00ff00 [blue] => 0000ff [red] => ff0000 ) */ $val = array_values($arr); print_r($val); /* Array ( [0] => 00ff00 [1] => 0000ff [2] => ff0000 ) */ |
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()
1 2 3 4 5 6 7 8 9 10 | $arr = array_intersect_key($colors, array_flip($rgb)); /* Array ( [red] => ff0000 [green] => 00ff00 [blue] => 0000ff ) */ |
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:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | $arr = array_intersect_key($colors, $bnw); /* Array ( [white] => ffffff [black] => 000000 ) */ $val = array_values($arr); /* Array ( [0] => ffffff [1] => 000000 ) */ |
3. Get Array Values by Keys with array_map()
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | function colors_using_array_map($clrs, $keys){ return array_map(function($x) use ($clrs) { return $clrs[$x]; }, $keys); } $arr = colors_using_array_map($colors, $rgb); /* Array ( [0] => 00ff00 [1] => 0000ff [2] => ff0000 ) */ |
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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | function array_exclude_keys($clrs, $keys){ foreach($keys as $k){ unset($clrs[$k]); } return $clrs; } $arr = array_exclude_keys($colors, $rgb); /* Array ( [white] => ffffff [black] => 000000 ) */ |
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:
1 2 3 4 5 6 7 8 | $arr = array_diff_key($colors, array_flip($rgb)); /* Array ( [white] => ffffff [black] => 000000 )*/ |
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.