1

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

1
  • I think you meant the keys from array1 would be 1 and 2. Commented Aug 13, 2013 at 14:40

3 Answers 3

2

Compare:

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

and

$rgResult = array_keys(array_intersect($array2, $array1));
Sign up to request clarification or add additional context in comments.

Comments

2

You're looking for array_intersect

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

Comments

0
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);

Comments

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.