如何基于一组键有效地提取子数组

时间:2013-01-08 20:02:02

标签: php arrays

我有一个数组数组$ data。

print_r($data); 

返回

Array ( 
[1401] => Array ( [0] => 94 [1] => 2 [2] => 159 ) 
[1402] => Array ( [0] => 94 [1] => 2 [2] => 50 [3] => 23 [4] => 159 ) 
[1403] => Array ( [0] => 94 [1] => 2 [2] => 50 ) 
[1404] => Array ( [0] => 94 [1] => 90 [2] => 50 [3] => 23 ) 
[1405] => Array ( [0] => 94 [1] => 90 ) 
[1406] => Array ( [0] => 94 [1] => 90 [2] => 23 ) 
[1407] => Array ( [0] => 94 [1] => 90 [2] => 50 )
) 

键是一组数字。我需要从这个数组中提取一个子数组,该数组只有密钥存储在另一个变量中。

$toextract=array(1402,1406);

除了使用循环逐个构建新数组之外,还有一种更简单的方法。我需要多次运行此命令,因此快速非常重要。

1 个答案:

答案 0 :(得分:6)

 $result = array_intersect_key($data, array_flip(array(1402, 1406));

array_intersect_key()