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

时间:2013-04-22 18:01:33

标签: php arrays string similarity

我有2个阵列:

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

我需要比较2个数组并将匹配元素的位置保存到第3个数组$arr3 = (3, 0, 2, 4, 5, 6); //expected result, displaying position of matching element of $arr1 in $arr2.

“匹配”是指所有相同的元素(例如世界),或者部分相同(例如测试和测试)以及那些相似但不同情况的元素(例如Foo& foo,Bar& bar)。

我尝试了一系列组合和各种功能但没有成功,使用array_intersect(), substr_compare(), array_filter()等功能。我不是要求确切的解决方案,只是为了让我走上正确的轨道,因为我整个下午都在四处走动。

4 个答案:

答案 0 :(得分:1)

这对我来说似乎有用,但我确信有一些边缘情况我不知道你需要测试:

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;
}

produces your desired output array

Array ( [0] => 3 [1] => 0 [2] => 2 [3] => 4 [4] => 5 [5] => 6 )  

答案 1 :(得分:0)

尝试使用实现“匹配”算法的回调函数array_uintersect()

答案 2 :(得分:0)

看起来你需要2个foreach循环和stripos(或Unicode的mb_stripos)进行比较。

答案 3 :(得分:0)

我想出了这个来解释重复。它返回键和两个值,因此您可以进行比较以查看它是否正常工作。要对其进行优化,您只需注释掉设置不需要的值的行。它符合所有情况。

<?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);


?>

输出如下:

        Array
(
    [1] => Array
        (
            [0] => Array
                (
                    [key1] => 1
                    [key2] => 0
                    [arr1] => Hello
                    [arr2] => hello
                )

        )

    [2] => Array
        (
            [2] => Array
                (
                    [key1] => 2
                    [key2] => 2
                    [arr1] => World
                    [arr2] => World
                )

        )

    [3] => Array
        (
            [4] => Array
                (
                    [key1] => 3
                    [key2] => 4
                    [arr1] => Foo
                    [arr2] => foo
                )

        )

    [4] => Array
        (
            [5] => Array
                (
                    [key1] => 4
                    [key2] => 5
                    [arr1] => Bar1
                    [arr2] => BaR1
                )

        )

    [5] => Array
        (
            [5] => Array
                (
                    [key1] => 5
                    [key2] => 5
                    [arr1] => Bar
                    [arr2] => BaR1
                )

            [6] => Array
                (
                    [key1] => 5
                    [key2] => 6
                    [arr1] => Bar
                    [arr2] => Bar
                )

        )

)