Marty Wallace
Marty Wallace

Reputation: 35744

php get matching keys between 2 arrays

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

Upvotes: 1

Views: 1472

Answers (1)

Mark Baker
Mark Baker

Reputation: 212442

$theListOfKeysWotIWant = array_keys(
    array_intersect_key(
        $array1,
        $array2
    )
);

Upvotes: 2

Related Questions