2

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.

1
  • Please provide a simple example of both arrays. Commented Jun 27, 2009 at 2:54

1 Answer 1

6
$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.

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

3 Comments

@Cletus - I think you meant to flip the $assoc as opposed to the plain $array.
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 +1 you're right, wasted your time on that one, sorry for that.

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.