假设我有一个array ("things and stuff", "the stuff");
我正在迭代以将每个元素与另一个数组的每个元素进行比较 - 假设第二个数组是("stuff the blah", "stuff and things to do");
我希望preg_match
任何通常相关的内容 - 我需要它来匹配单词相同的元素,即使它们的位置已被更改 - 基本上我希望"the stuff"
匹配{ {1}}在这种情况下。
最好的方法是什么?
答案 0 :(得分:0)
您可以简单地将第一个数组的字符串拆分为与第二个数组上的所有字符串值进行比较,此处为示例:
$array1 = array("things and stuff", "the stuff");
$array2 = array("stuff the blah", "stuff and things to do");
$result = array();
foreach($array1 as $val1){
$words1 = explode(" ", $val1);
$result[$val1] = 'not found';
foreach($array2 as $val2){
$words2 = explode(" ", $val2);
$intersect = array_intersect($words1, $words2);
$diff = array_diff($words1, $intersect);
if(empty($diff))
$result[$val1] = 'found';
}
}
var_dump($result);
参考文献: