Question

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

Was it helpful?

Solution

$theListOfKeysWotIWant = array_keys(
    array_intersect_key(
        $array1,
        $array2
    )
);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top