Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to do PHP array_intersect by keys not by values?

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

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

I want to remove all keys from $master that do not exists in $check. So the result in this example should be:

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

Any idea how to do this ? Thanks in advance.

like image 744
black-room-boy Avatar asked Sep 25 '22 17:09

black-room-boy


1 Answers

Yes, simply use array_intersect_key()

$newMaster = array_intersect_key($master, $check);
like image 187
Havelock Avatar answered Sep 28 '22 23:09

Havelock