简体   繁体   中英

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM