假设我有一个array ("things and stuff", "the stuff");
我正在迭代以将每个元素与另一个数组的每个元素进行比较 - 假设第二个数组是("stuff the blah", "stuff and things to do");
我想要preg_match
任何通常相关的东西——我需要它来匹配单词相同的元素,即使它们的位置已经改变——本质上我想在这种情况下"the stuff"
匹配。"stuff the..."
解决这个问题的最佳方法是什么?
假设我有一个array ("things and stuff", "the stuff");
我正在迭代以将每个元素与另一个数组的每个元素进行比较 - 假设第二个数组是("stuff the blah", "stuff and things to do");
我想要preg_match
任何通常相关的东西——我需要它来匹配单词相同的元素,即使它们的位置已经改变——本质上我想在这种情况下"the stuff"
匹配。"stuff the..."
解决这个问题的最佳方法是什么?
您可以简单地拆分第一个数组的字符串以与第二个数组上的所有字符串值进行比较,这里是示例:
$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);
参考: