К моему огромному удивлению, метод цикла foreach работает быстрее.
Следующий скрипт быстрого тестирования дает мне результаты: array_intersect_key: 0.76424908638
foreach loop: 0.6393928527832
$A = array('a'=>'book', 'b'=>'pencil', 'c'=>'pen');
$B = array('a', 'b');
$start = microtime(true);
for ($i = 0 ; $i < 1000000; $i++) {
$c = array_intersect_key($A,array_flip($B));
}
$t1 = microtime(true);
for ($i = 0; $i < 1000000; $i++) {
$C = array();
foreach ($B as $bval) {
// If the $B key exists in $A, add it to $C
if (isset($A[$bval])) $C[$bval] = $A[$bval];
}
}
$t2 = microtime(true);
echo "array_intersect_key: " . ($t1 - $start), "\n";
echo "foreach loop: " . ($t2 - $t1), "\n";