简体   繁体   中英

PHP Get keys from one array of intersected values

in php, if I have two arrays:

array1 = array('a', 'b', 'c');
array2 = array('b', 'c');

Is there a function of combination of functions that will compare the two arrays' values and return the keys from 1 array of the intersection?

If wanting the key s from array1 , they would be 1 and 2 If wanting the key s from array2 , they would be 0 and 1

Compare:

$rgResult = array_keys(array_intersect($array1, $array2));

and

$rgResult = array_keys(array_intersect($array2, $array1));

你正在寻找array_intersect

array_keys(array_intersect($array1, $array2));
array_keys($array1 = array('a', 'b', 'c'); $array2 = array('b', 'c');

$intersection = array_intersect($array1, $array2);

$keys = array();
foreach($intersection as $i){
    $keys[]= array_search($i,$array1);
}
print_r($keys);

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