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
0

1 Answer 1

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

Sign up to request clarification or add additional context in comments.

3 Comments

Deleted my answer, which was exactly the same, as you had answered this first.
Whoa. Rocket's quite fast. Beat me. +1 :)
@AmalMurali: I'm a ninja ^~^

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.