【问题标题】:PHP - How do I partially compare elements in 2 arraysPHP - 如何部分比较 2 个数组中的元素
【发布时间】:2013-04-22 18:01:32
【问题描述】:

我有 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,酒吧和酒吧)。

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

【问题讨论】:

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

标签: php arrays string similarity


【解决方案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 )  

【讨论】:

    【解决方案2】:

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

    【讨论】:

      【解决方案3】:

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

      【讨论】:

        【解决方案4】:

        我想出了这个来解决重复问题。它返回两个键和两个值,以便您可以比较它是否正常工作。要对其进行改进,您只需注释掉设置不需要的值的行。它匹配所有情况。

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

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2020-07-25
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多