1

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

2
  • 1
    php.net/manual/en/function.array-intersect-key.php Commented Jan 30, 2013 at 14:55
  • what exactly to you want as result? please update your post with expected result (array) manually Commented Jan 30, 2013 at 15:04

1 Answer 1

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

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.