82

I am searching for a built in php function that takes array of keys as input and returns me corresponding values.

for e.g. I have a following array

$arr = array("key1"=>100, "key2"=>200, "key3"=>300, 'key4'=>400);

and I need values for the keys key2 and key4 so I have another array("key2", "key4") I need a function that takes this array and first array as inputs and provide me values in response. So response will be array(200, 400)

2
  • 2
    I don't understand exactly what you are asking. Can you clarify, maybe with an example input and output? Commented Nov 21, 2010 at 20:25
  • If it doesn't have to be a built in function, then @Amber has the best answer. There doesn't appear to be a built-in function that does precisely what you're asking for. Commented Nov 22, 2022 at 15:25

3 Answers 3

170

I think you are searching for array_intersect_key. Example:

$arr = [ "key1"=>100, "key2"=>200, "key3"=>300, 'key4'=>400 ];
$keys = [ "key2", "key4" ];
print_r(
    array_intersect_key(
        $arr,
        array_flip($keys)
    )
);

Would return:

Array ( [key2] => 200 [key4] => 400 )

You may use ['key2' => '', 'key4' => '' instead of array_flip(...) if you want to have a little simpler code.

Note the array keys are preserved. You should use array_values afterwards if you need a sequential array.

Sign up to request clarification or add additional context in comments.

7 Comments

great answer, is there any shorter way?
Don't think so. You may write array('a'=>'', 'c'=>'') instead of array_flip(...), but it seems to have different semantic for me.
Note that this returns an associative array (so in the example of the OP, it wouldn't return array(200,400), but instead array('key2'=>200,'key4'=>400).
@Amber, does it really matter if it returns array(0=>100,1=>400) or array('key2'=>200,'key4'=>400)? But in any way, if the resulting array should not display the original keys, just wrap that one liner inside a array_values()
@Yanick: There are edge cases where it can matter (consider: foreach($arr as $index => $val) { ...stuff using $index... }).
|
29

An alternative answer:

$keys = array("key2", "key4");

return array_map(function($x) use ($arr) { return $arr[$x]; }, $keys);

1 Comment

If efficiency matters, this solution is calling a function for every element in $keys, which is unnecessarily inefficient. @Amber has the better answer.
10
foreach($input_arr as $key) {
    $output_arr[] = $mapping[$key];
}

This will result in $output_arr having the values corresponding to a list of keys in $input_arr, based on the key->value mapping in $mapping. If you want, you could wrap it in a function:

function get_values_for_keys($mapping, $keys) {
    foreach($keys as $key) {
        $output_arr[] = $mapping[$key];
    }
    return $output_arr;
}

Then you would just call it like so:

$a = array('a' => 1, 'b' => 2, 'c' => 3);
$values = get_values_for_keys($a, array('a', 'c'));
// $values is now array(1, 3)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.