我有一个如下所示的数组:
Array
(
[333824-00A-BLK-10] => Array
(
[classId] => 44
[inv] => 0.000
[onOrder] => 0
[code] => 333824-00A-BLK-10
)
[333824-00A-BLK-10.5] => Array
(
[classId] => 44
[inv] => 0.000
[onOrder] => 0
[code] => 333824-00A-BLK-10.5
)
)
我想使用另一个如下所示的数组来过滤它:
Array
(
[0] => 333824-00A-BLK-10
[1] => 333824-00A-BLK-10.5
[2] => 333824-00A-BLK-11
[3] => 333824-00A-BLK-11.5
[4] => 333824-00A-BLK-12
)
我想将结果保留在数组中,并删除不匹配的键。我尝试过一个用foreach过滤数组但没有运气的函数。有什么帮助吗?谢谢!
答案 0 :(得分:6)
$result = array_intersect_key($data, array_flip($keys));
$data
是您的主数组,而$keys
是一个包含要过滤的键的数组
答案 1 :(得分:0)
我会做一个虚拟数组并在foreach中过滤它,我不知道你的foreach看起来如何,但我的看起来像
$arr = array();
$filter_array = array ('333824-00A-BLK-10',
'333824-00A-BLK-10.5',
'333824-00A-BLK-11',
'333824-00A-BLK-11.5',
'333824-00A-BLK-12');
$array_to_filter = array('333824-00A-BLK-10' => array
(
'classId' => 44,
'inv' => 0.000,
'onOrder' => 0,
'code' => '333824-00A-BLK-10'
),
'333824-00A-BLK-10.5' => array
(
'classId' => 44,
'inv' => 0.000,
'onOrder' => 0,
'code' => '333824-00A-BLK-10.5'
)
);
foreach($array_to_filter as $filter) {
if(array_key_exists($filter, $array_to_filter)) {
$arr[] = $array_to_filter[$filter];
}
}
<$>在$ arr中,您现在应该拥有数组中的所有现有键