Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

What is the best way to get the matching keys between two associative arrays:

Array (
    [array_1] => Array (
        [abc] => 111
        [def] => 222
    ),
    [array_2] => Array (
        [ghi] => 995
        [jkl] => 996
        [mno] => 997
    )
)

and

Array (
    [array_1] => Array (
        [123] => 111
        [345] => 222
    ),
    [array_2] => Array (
        [123] => 995
        [432] => 996
        [345] => 997
    ),
    [array_3] => Array (
        [456] => 995
        [345] => 996
        [234] => 997
    )
)

I would like an array to be returned containing only the values: array_1 and array_2.

array_intersect doesn't really work here neither will array_intersect_key as it will return the child array

I want this as a result:

array('array_1','array_2')

since these are the keys that match

share|improve this question
1  
 
what exactly to you want as result? please update your post with expected result (array) manually –  user1646111 Jan 30 at 15:04
add comment

2 Answers

up vote 2 down vote accepted
$theListOfKeysWotIWant = array_keys(
    array_intersect_key(
        $array1,
        $array2
    )
);
share|improve this answer
add comment

You can try a combination of array_keys() and array_intersect().

For example:

$shared_keys = array_intersect(array_keys($array1),arrays_keys($array2));

The key here (no pun intended) is that array_keys() allows you to manipulate the keys of an array as an array in themselves.

share|improve this answer
add comment

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.