tags:

views:

714

answers:

1

I have a set of ids and names in an associative array and in my other array I have my list of id's that I want to compare against the first list.

I'd like to be able to perform an intersection type search function without losing the names from the associative array.

I've though about doing a nested foreach, but it seems like this process could take forever as both arrays could potentially have 70k+ values.

+5  A: 
$assoc = array(
  'a' => 'one',
  'b' => 'two',
);
$array = array('b', 'c', 'd');
$match = array_intersect_key($assoc, array_flip($array));
print_r($match);

outputs:

Array
(
    [b] => two
)

which I believe is what you're after.

cletus
@Cletus - I think you meant to flip the $assoc as opposed to the plain $array.
karim79
No, the above is what I intended. It returns the elements from the assoc whos keys are values in $array, which I believe is what the OP wants. It does so while preserving the values in the assoc. If not, I can correct.
cletus
@cletus +1 you're right, wasted your time on that one, sorry for that.
karim79
No worries . :)
cletus
Thanks that is great. Exactly what I needed!
Tim