PHP-如何部分比较两个数组中的元素

PHP-如何部分比较两个数组中的元素,php,arrays,string,similarity,Php,Arrays,String,Similarity,我有两个阵列: $arr1 = array('Test', 'Hello', 'World', 'Foo', 'Bar1', 'Bar'); and $arr2 = array('hello', 'Else', 'World', 'Tes', 'foo', 'BaR1', 'Bar'); 我需要比较两个数组,并将匹配元素的位置保存到第三个数组$arr3=(3,0,2,4,5,6)//预期结果,显示匹配元素$arr1在$arr2中的位置。 所谓“匹配”,我指的是所有相同(例如World)或部分

我有两个阵列:

$arr1 = array('Test', 'Hello', 'World', 'Foo', 'Bar1', 'Bar'); and
$arr2 = array('hello', 'Else', 'World', 'Tes', 'foo', 'BaR1', 'Bar'); 
我需要比较两个数组,并将匹配元素的位置保存到第三个数组
$arr3=(3,0,2,4,5,6)//预期结果,显示匹配元素$arr1在$arr2中的位置。

所谓“匹配”,我指的是所有相同(例如World)或部分相同(例如Test&Tes)的元素,以及那些相似但情况不同的元素(例如Foo&Foo、Bar&Bar)


我尝试了一系列组合和各种函数,但没有成功,使用了诸如
array\u intersect()、substr\u compare()、array\u filter()等函数。我不是在要求确切的解决方案,只是为了让我走上正确的轨道,因为我整个下午都在兜圈子。

尝试使用一个实现“匹配”算法的回调函数。

尝试使用一个实现“匹配”算法的回调函数。

看起来您需要两个foreach循环和stripos(或mb_stripos for Unicode)用于比较。

看起来您需要2个foreach循环,而stripos(或mb_stripos for Unicode)用于比较。

这似乎对我很有效,但我确信我缺少一些边缘情况,您需要测试:

foreach( $arr1 as $i => $val1) {
    $result = null;
    // Search the second array for an exact match, if found
    if( ($found = array_search( $val1, $arr2, true)) !== false) {
            $result = $found; 
    } else {
        // Otherwise, see if we can find a case-insensitive matching string where  the element from $arr2 is at the 0th location in the one from $arr1
        foreach( $arr2 as $j => $val2) {            
            if( stripos( $val1, $val2) === 0) {
                $result = $j;
                break;
            }
        }
    }
    $arr3[$i] = $result;
}
它:


这似乎对我有效,但我确信我遗漏了一些边缘情况,您需要测试:

foreach( $arr1 as $i => $val1) {
    $result = null;
    // Search the second array for an exact match, if found
    if( ($found = array_search( $val1, $arr2, true)) !== false) {
            $result = $found; 
    } else {
        // Otherwise, see if we can find a case-insensitive matching string where  the element from $arr2 is at the 0th location in the one from $arr1
        foreach( $arr2 as $j => $val2) {            
            if( stripos( $val1, $val2) === 0) {
                $result = $j;
                break;
            }
        }
    }
    $arr3[$i] = $result;
}
它:


我提出这个方法是为了解释重复。它返回两个键和两个值,以便您可以比较它是否正常工作。要优化它,您只需注释掉设置不需要的值的行。它匹配所有情况

<?php

$arr1 = array('Test', 'Hello', 'World', 'Foo', 'Bar1', 'Bar');
$arr2 = array('hello', 'Else', 'World', 'Tes', 'foo', 'BaR1', 'Bar');

$matches = array();
//setup the var for the initial match
$x=0;
foreach($arr1 as $key=>$value){

$searchPhrase = '!'.$value.'!i';

    //Setup the var for submatching (in case there is more than one)
    $y=0;
    foreach($arr2 as $key2=>$value2){
        if(preg_match($searchPhrase,$value2)){
            $matches[$x][$y]['key1']=$key;
            $matches[$x][$y]['key2']=$key2;
            $matches[$x][$y]['arr1']=$value;
            $matches[$x][$y]['arr2']=$value2;   
        }
        $y++;
    }
    unset($y);
    $x++;
}

print_r($matches);


?>

我提出这个方法是为了解释重复。它返回两个键和两个值,以便您可以比较它是否正常工作。要优化它,您只需注释掉设置不需要的值的行。它匹配所有情况

<?php

$arr1 = array('Test', 'Hello', 'World', 'Foo', 'Bar1', 'Bar');
$arr2 = array('hello', 'Else', 'World', 'Tes', 'foo', 'BaR1', 'Bar');

$matches = array();
//setup the var for the initial match
$x=0;
foreach($arr1 as $key=>$value){

$searchPhrase = '!'.$value.'!i';

    //Setup the var for submatching (in case there is more than one)
    $y=0;
    foreach($arr2 as $key2=>$value2){
        if(preg_match($searchPhrase,$value2)){
            $matches[$x][$y]['key1']=$key;
            $matches[$x][$y]['key2']=$key2;
            $matches[$x][$y]['arr1']=$value;
            $matches[$x][$y]['arr2']=$value2;   
        }
        $y++;
    }
    unset($y);
    $x++;
}

print_r($matches);


?>
您可以使用
strtolower($str)
匹配小写和大写。使用正则表达式匹配部分字符串您可以使用
strtolower($str)
匹配小写和大写。使用正则表达式匹配部分字符串