确定在经历了许多类似的问题后发布此查询,但他们只考虑了数值。所以我的问题是:
我有2个数组 -
$a = ["XYZ1250H100",
"XYZ1280H130",
"XYZ1250H150",
"XYZ3300H200",
"XYZ3350H200",
"XYZ33350H280Ada08",
"XYZ33450H300Ada08",
"XYZ33508H406Ada08"];
和
$b = ["XYZ0L200H150A4c00",
"350L610H457Ada08",
"XYZ33762H610Ada08",
"350L914H610Ada08",
"3700L250H200A410b",
"XYZ33457H305Ada08",
"XYZ4550H100MMQOJ",
"XYZ4580H130Ada08",
"XYZ4550H150A69b5",
"3101L280H356A8b83",
"XYZ4550H1501FC5Z",
"3116L150H15074QFR",
"XYZ1250H200A21ac",
"3101L750H500A8b83",
"350L356H279Ada08",
"XYZ1250H200A3f1c",
"3700L153H102A8d96",
"XYZ4550H150Ada08",
"XYZ4580H130A69b5",
"350L1830H610Ada08",
"3700L153H102A4c00",
"XYZ4550H150STD9J",
"3800L200H1505CZJI",
"XYZ4550H100A69b5",
"XYZ331370H450Ada08"
];
我需要查看数组$ a中数组$ a的值是否存在,我尝试使用array_diff()和array_intersect(),但这并没有解决问题。我想要的是如果2个数组匹配,那么它应该返回true或false。例如,如果我们考虑$ a中的价值 - > " XYZ1280H130"价值在$ b - > " XYZ1280H130A69b5"那么它应该被认为是一个匹配的值(coz前者是后一个值的子串),而true应该是return并且对于所有相似的值都是相同的。
我尝试了类似这样的东西,但它每次都会返回数组$ b的所有值:
$result = array_filter($a, function($item) use($b) {
foreach($b as $substring)
if(strpos($item, $substring) !== FALSE) return TRUE;
return FALSE;
});
如果我错过了什么,请告诉我。提前谢谢。
答案 0 :(得分:1)
使用正则表达式:
foreach($b as $search){
foreach($a as $sub){
if(preg_match("/$sub/",$search)){ echo "found ".$sub." in ".$search;}
}
}
答案 1 :(得分:0)
您可以使用array_map。 但这应该会给你你想要的结果:
function test($a, $b) {
foreach($a as $first) {
foreach($b as $second) {
if(strpos($b, $a) !== false) return true;
}
}
}
答案 2 :(得分:0)
修改了@ clearshot66上面的一个答案,以获得所需的结果:
function match_them($a, $b){
foreach($b as $search){
foreach($a as $key => $val){
if(preg_match("/$val/",$search)){unset($a[$key]);}
}
}
return count($a);
}
var_dump(match_them($a, $b));