Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

Given two arrays:

$foo = array('a', 'b', 'c');
$bar = array('a' => 1, 'b' => 2, 'c' => 3, 'd' => 4, 'e' => 5);

Is there a built-in PHP function to produce the following result array?

$result = array('a' => 1, 'b' => 2, 'c' => 3);

I've been through the Array Functions list on php.net, but can't seem to find what I'm looking for. I know how to do it myself if need be, but I figured this might be a common-enough problem that there might be a built-in function that does it and didn't want to reinvent the wheel.

share|improve this question
    
Basically pull out the key/val pairs from $bar where bar's keys have a matching value in $foo? –  Marc B Jan 4 '12 at 14:37
    
To Clarify : So you want to extract the values from the second array that have keys that match the first array ? –  ManseUK Jan 4 '12 at 14:37
    
Yes, I want a resulting array that has the elements from the second array ($bar), but only the keys from the first array ($foo). –  drrcknlsn Jan 4 '12 at 14:40
    
And you know how to accomplish this with a simple iteration, you just want to know if there's a specific array function to handle it? –  rdlowrey Jan 4 '12 at 14:42
    
@rdlowrey: That's correct. –  drrcknlsn Jan 4 '12 at 14:43

3 Answers 3

up vote 5 down vote accepted

Another way using array_flip and array_intersect_keys:

$foo = array('a', 'b', 'c'); 
$bar = array('a' => 1, 'b' => 2, 'c' => 3, 'd' => 4, 'e' => 5);

$common = array_intersect_key($bar, array_flip($foo));

Output

array(3) {
  ["a"]=>
  int(0)
  ["b"]=>
  int(1)
  ["c"]=>
  int(2)
}
share|improve this answer
    
reverse the order of arguments to array_intersect_key() to maintain the values from the original $bar array –  Mark Baker Jan 4 '12 at 14:51
    
@Mark Baker. Good call! Thanks. –  Josh Jan 4 '12 at 14:54
    
This is very similar to the solution I posted, but I'm picking it because of your use of array_flip($foo) over my use of array_fill_keys($foo, NULL). –  drrcknlsn Jan 4 '12 at 15:34

It's a bit of a dirty hack, but it works:

function extractKeys($keys, $data) {
    extract($data);

    return call_user_func_array('compact', $keys);
}

$foo = array('a', 'b', 'c');
$bar = array('a' => 1, 'b' => 2, 'c' => 3, 'd' => 4, 'e' => 5);

var_dump(extractKeys($foo, $bar));

Output:

array(3) {
  ["a"]=>
  int(1)
  ["b"]=>
  int(2)
  ["c"]=>
  int(3)
}
share|improve this answer

After posting, I thought of one way of doing this:

array_intersect_key($bar, array_fill_keys($foo, NULL))

Though, this isn't really the concise, built-in function that I had hoped for, it's definitely better than constructing the resultant array manually.

share|improve this answer
2  
array_fill_keys($foo, NULL) can be replaced with array_flip($foo) –  Mark Baker Jan 4 '12 at 14:47
    
@MarkBaker: I had never thought of using array_flip() on a non-associative array. That's a neat trick, thanks. –  drrcknlsn Jan 4 '12 at 14:50
    
best type of array to use it on.... no risk of duplicate values leading to overwriting keys when it's flipped –  Mark Baker Jan 4 '12 at 14:53

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.