vote up 1 vote down star

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.

flag

Please provide a simple example of both arrays. – Matthew Flaschen Jun 27 at 2:54

1 Answer

vote up 5 vote down check
$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.

link|flag
@Cletus - I think you meant to flip the $assoc as opposed to the plain $array. – karim79 Jun 27 at 3:29
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 Jun 27 at 4:11
@cletus +1 you're right, wasted your time on that one, sorry for that. – karim79 Jun 27 at 4:44
No worries . :) – cletus Jun 27 at 4:47
Thanks that is great. Exactly what I needed! – Tim Jun 27 at 5:57

Your Answer

Get an OpenID
or

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