1

I have following arrays:

$keys

array (size=2)
  0 => string 'foo' (length=3)
  1 => string 'buz' (length=3)

$data

array (size=3)
  'foo' => int 1
  'bar' => int 2
  'buz' => int 3

How to get $data array filtered by $keys values ? Desired output:

array (size=3)
  'foo' => int 1
  'buz' => int 3
hsz
  • 148,279
  • 62
  • 259
  • 315

1 Answers1

5

array_intersect_key should be able to help you out here

array_intersect_key($data, array_flip($keys));

The array_flip is needed because array_intersect_key operates on keys, so this makes sure both arrays are in the right format.

DEMO: http://codepad.org/AGpDAZtE

gen_Eric
  • 223,194
  • 41
  • 299
  • 337