Tim
Tim

Reputation: 982

Compare associative array with standard array values PHP

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.

Upvotes: 2

Views: 1800

Answers (1)

cletus
cletus

Reputation: 625237

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

Upvotes: 6

Related Questions