0

Is there a built-in function for:

print_r(array_***([
    'foo' => 1,
    'bar' => 2,
    'baz' => 3,
    'qux' => 4,
], ['foo', 'qux']));

to produce:

Array
(
    [foo] => 1
    [qux] => 4
)

?

Slava Fomin II
  • 26,865
  • 29
  • 124
  • 202

1 Answers1

0

What you're looking for is the array_intersect_key function (http://www.php.net/manual/en/function.array-intersect-key.php).

Running:

var_dump(array_intersect_key(array('foo'=>1,'bar'=>2,'baz'=>3,'qux'=>4),array('foo'=>true,'qux'=>true)));

Should give you:

array(2) { ["foo"]=> int(1) ["qux"]=> int(4) }
robbymarston
  • 344
  • 3
  • 16