Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

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 keys from array1, they would be 1 and 2 If wanting the keys from array2, they would be 0 and 1

share|improve this question
 
I think you meant the keys from array1 would be 1 and 2. –  Pé de Leão Aug 13 at 14:40
 
Yes, thanks, fixed it. –  Ayen Aug 13 at 15:02

3 Answers

up vote 2 down vote accepted

Compare:

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

and

$rgResult = array_keys(array_intersect($array2, $array1));
share|improve this answer

You're looking for array_intersect

array_keys(array_intersect($array1, $array2));
share|improve this answer
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);
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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