5

$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.

black-room-boy
  • 659
  • 2
  • 11
  • 23

3 Answers3

4

Yes, simply use array_intersect_key()

$newMaster = array_intersect_key($master, $check);
Havelock
  • 6,913
  • 4
  • 34
  • 42
0

Yeah you can simply use:

var_dump(array_intersect_key($master, $check));
Waqas Shahid
  • 1,051
  • 1
  • 7
  • 22
0
$master = ['111' => 'foo', '124' => 'bar', '133' => 'baz'];

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


$intersectArray = array_intersect_key($master, $check);

Here key will compare using array_intersect_key() function it will compare your $check key in $master and give you result where $check key is matched in $master and you got output ['111' => 'foo', '133' => 'baz']; in $intersectArray

For more details you will check this link http://php.net/manual/en/function.array-intersect-key.php

jilesh
  • 436
  • 1
  • 3
  • 13