1

我有 2 个数组:

$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); //expected result, displaying position of matching element of $arr1 in $arr2.

“匹配”是指所有相同的元素(例如 World)或部分相同的元素(例如 Test 和 Tes),以及那些相似但大小写不同的元素(例如 Foo 和 foo、Bar 和 bar )。

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

4

4 回答 4

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

产生您想要的输出数组

Array ( [0] => 3 [1] => 0 [2] => 2 [3] => 4 [4] => 5 [5] => 6 )  
于 2013-04-22T18:15:12.217 回答
0

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

于 2013-04-22T18:06:37.000 回答
0

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

于 2013-04-22T18:11:19.550 回答
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
                )

        )

)
于 2013-04-22T18:29:32.460 回答