PHP如何通过键而不是值进行array_intersect?

bla*_*boy 5 php arrays array-intersect

$master = ['111' => 'foo', '124' => 'bar', '133' => 'baz'];

$check = ['111' => 14, '133' => 23 ]';

我想从中删除所有$master不存在的键$check。因此,此示例中的结果应为:

$newMaster = ['111' => 'foo', '133' => 'baz'];

任何想法如何做到这一点?提前致谢。

Hav*_*ock 4

是的,只需使用array_intersect_key()

$newMaster = array_intersect_key($master, $check);
Run Code Online (Sandbox Code Playgroud)