php获取2个数组之间的匹配键

时间:2013-01-30 14:51:11

标签: php arrays array-intersect array-key

在两个关联数组之间获取匹配键的最佳方法是什么:

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

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

我希望返回的数组只包含以下值:array_1array_2

array_intersect在这里不起作用array_intersect_key也不会,因为它会返回子数组

我希望这样做:

array('array_1','array_2')

因为这些是匹配的键

1 个答案:

答案 0 :(得分:2)

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