是否有内置功能:
print_r(array_***([
'foo' => 1,
'bar' => 2,
'baz' => 3,
'qux' => 4,
], ['foo', 'qux']));
生产:
Array
(
[foo] => 1
[qux] => 4
)
答案 0 :(得分:0)
您正在寻找的是array_intersect_key函数(http://www.php.net/manual/en/function.array-intersect-key.php)。
运行:
var_dump(array_intersect_key(array('foo'=>1,'bar'=>2,'baz'=>3,'qux'=>4),array('foo'=>true,'qux'=>true)));
应该给你:
array(2) { ["foo"]=> int(1) ["qux"]=> int(4) }