2013-04-22 49 views
0

比較元素我有2個數組:PHP - 如何部分在2個陣列

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

我需要比較2個陣列和匹配元件的位置保存到第三陣列$arr3 = (3, 0, 2, 4, 5, 6); //expected result, displaying position of matching element of $arr1 in $arr2.

「匹配」是指所有相同(例如世界)或部分相同的元素(例如測試& Tes)以及那些相似但具有不同情況的元素(例如Foo & foo,Bar & bar )。

我試過了一系列的組合和各種功能沒有成功,使用像array_intersect(), substr_compare(), array_filter()等功能。我沒有要求確切的解決方案,只是爲了讓我走上正確的軌道,因爲我整個下午都在圈子裏走動。

+0

你可以用'用strtolower($ STR)'匹配小寫和大寫。使用正則表達式來匹配部分字符串 – 2013-04-22 18:08:12

回答

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) 
+0

這看起來像它會從我身上運行,只要我用真實數據對其進行測試,我會立即通知您,謝謝。 – bikey77 2013-04-22 18:49:00

+0

檢查真實的數據並且正常工作,謝謝。 – bikey77 2013-04-23 17:07:30

0

看起來你需要2個foreach循環,stripos(或用於Unicode的mb_stripos)用於比較。

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 
       ) 

     ) 

)