$my_array = ['foo' => 1, 'hello' => 'world']; $allowed = ['foo', 'bar']; $filtered = array_filter( $my_array, function ($key) use ($allowed) { return in_array($key, $allowed); }, ARRAY_FILTER_USE_KEY );