php 一个多维数组根据一维数组筛选数据,得到新数组
数组A:Array([BL001]=>Array([0]=>'A14257',[1]=>'B17548')[CDF002]=>Array([0]=>'A14257',[1...
数组A:
Array( [BL001] => Array([0] => 'A14257',[1] => 'B17548') [CDF002] => Array([0] => 'A14257',[1] => 'C48751')
[FGE453] => Array([0] => 'DF5533' ,[1] => 'GR6445',[2] => 'GH5544')
[GEN001] => Array([0] => 'NT7532' ,[1] => 'DE9342',[2] => 'C48751'))
数组B:
Array([0] => 'A14257',[1] => 'E453T4',[2] => 'Y64344',[3] => 'FG4809',[4] => 'C48751',[5] => 'H4443R')
如果数组A含有数组B的数据就生成新的数组,不包含的话删除,结果是这样:
Array( [BL001] => Array([0] => 'A14257') [CDF002] => Array([0] => 'A14257',[1] => 'C48751') [GEN001] => Array([0] => 'C48751')) 展开
Array( [BL001] => Array([0] => 'A14257',[1] => 'B17548') [CDF002] => Array([0] => 'A14257',[1] => 'C48751')
[FGE453] => Array([0] => 'DF5533' ,[1] => 'GR6445',[2] => 'GH5544')
[GEN001] => Array([0] => 'NT7532' ,[1] => 'DE9342',[2] => 'C48751'))
数组B:
Array([0] => 'A14257',[1] => 'E453T4',[2] => 'Y64344',[3] => 'FG4809',[4] => 'C48751',[5] => 'H4443R')
如果数组A含有数组B的数据就生成新的数组,不包含的话删除,结果是这样:
Array( [BL001] => Array([0] => 'A14257') [CDF002] => Array([0] => 'A14257',[1] => 'C48751') [GEN001] => Array([0] => 'C48751')) 展开
展开全部
<?php
// 定义数组 1
$arr1 = array(
'BL001' => array(
'0' => 'A14257',
'1' => 'B17548'
),
'CDF002' => array(
'0' => 'A14257',
'1' => 'C48751'
),
'FGE453' => array(
'0' => 'DF5533',
'1' => 'GR6445',
'2' => 'GH5544'
),
'GEN001' => array(
'0' => 'NT7532',
'1' => 'DE9342',
'2' => 'C48751'
)
);
// 定义数组 2
$arr2 = array(
'A14257',
'E453T4',
'Y64344',
'FG4809',
'C48751',
'H4443R'
);
// 定义结果数组 $result
$result = array();
foreach ( $arr1 as $k => $v ) // 遍历数组 1
foreach ( $v as $s ) // 遍历数组 1 第 2 维
if ( in_array( $s, $arr2 ) ) { // 包含数据 2 的任意值
if ( ! isset( $result[$k] ) || ! is_array( $result[$k] ) )
$result[$k] = array();
$result[$k][] = $s; // 添加到结果数组
}
// 输出测试结果
print_r( $result );
/*
Array
(
[BL001] => Array
(
[0] => A14257
)
[CDF002] => Array
(
[0] => A14257
[1] => C48751
)
[GEN001] => Array
(
[0] => C48751
)
)
*/
展开全部
<?php
$a = array(
'BL001'=>array('A14257','B17548'),
'CDF002'=>array('A14257','C48751'),
'FGE453'=>array('DF5533','GR6445','GH5544'),
'GEN001'=>array('NT7532','DE9342','C48751'),
);
$b = array('A14257','E453T4','Y64344','FG4809','C48751','H4443R');
foreach($a as $k=>$v){
$t = array_intersect ($v, $b);
if($t){
$c[$k] = $t;
}
}
print_r($c);
?>
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
<?php
$a = array(
'BL001' => array('A14257','B17548'),
'CDF002' => array('A14257','C48751'),
'FGE453' => array('DF5533','GR6445','GH5544'),
'GEN001' => array('NT7532','DE9342','C48751')
);
$b = array('A14257','E453T4','Y64344','FG4809','C48751','H4443R');
$c = array();
foreach($b as $k=>$v){
foreach($a as $key=>$val){
if(in_array($v,$val)){
$c[$key][] = $v;
}
}
}
echo '<pre>';
var_dump($c);
?>
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询