2015-07-28 192 views
4

我有兩個數組在其中,我需要比較兩個數組並返回不匹配的數組。PHP比較數組並返回不同

我有兩個數組,這兩個看起來像這樣:

$arr1 = array(
      array('name' => 'Alan', 'age' => '34', 'country' => 'usa'), 
      array('name' => 'James', 'age' => '24', 'country' => 'spain'), 
      ); 

$arr2 = array(
      array('name' => 'Alan', 'age' => '34', 'country' => 'usa'), 
      array('name' => 'James', 'age' => '54', 'country' => 'spffain'), 
     ); 

我想通過比較姓名,年齡和國家的數組並返回我不匹配的數組。

到目前爲止我的代碼:

$intersect = array_uintersect($arr1, $arr2, 'compareDeepValue'); 
echo "<pre>", print_r($intersect); 

function compareDeepValue($val1, $val2) 
{ 
    return strcmp($val1['age'], $val2['age']); 
    return strcmp($val1['country'], $val2['country']); 
    return strcmp($val1['name'], $val2['name']); 

} 

上面的代碼返回匹配的數組。我如何更改代碼以獲取不匹配的數組?

預期輸出:

Array 
(
    [0] => Array 
     (
      [name] => James 
      [age] => 21 
      [country] => spain 
     ) 

) 
+1

可以使用的strcmp($ VAL1 [ '年齡'],$ VAL2 [ '年齡']); –

+0

檢查這個帖子:http://stackoverflow.com/questions/12169821/compare-differences-in-multidimensional-array – Javi

+1

你的'compareDeepValue'有3個返回語句。首先是難以到達的。 –

回答

3

有人在回答將工作中提到的代碼,但它是體力勞動:) 你可以使用現有的功能來爲你做的工作。對於計算陣列之間的差異,應該使用array_udiff函數(或函數相關)。

你應該寫函數比較數組,並用它來計算的區別,就像這樣:

<?php 
$arr1 = array(
     array('name' => 'Alan', 'age' => '34', 'country' => 'usa'), 
     array('name' => 'James', 'age' => '24', 'country' => 'spain'), 
     ); 

$arr2 = array(
     array('name' => 'Alan', 'age' => '34', 'country' => 'usa'), 
     array('name' => 'James', 'age' => '54', 'country' => 'spffain'), 
    ); 

// this function checks if 2 arrays with keys and scalar values are the same 
function array_same_check($deepArr1, $deepArr2) { 
    //check if arrays are the same 
    $diffArr = array_diff_assoc($deepArr1, $deepArr2); 
    //if $diffArr has 0 elements - arrays are the same 
    if (count($diffArr) === 0) { 
     return 0; 
    } 
    // arrays are not the same - return arbitratry 1 or -1 
    return 1; 
} 

// now let's compare $arr1 and $arr2 you have 
// (meaning: compare the difference between arrays containing arrays) - we use function above 
$differing = array_udiff ($arr1, $arr2, 'array_same_check'); 

print_r($differing); 

我複製此代碼PHPFiddle,它似乎按預期方式工作。

+0

感謝您的幫助.. –

+0

@Kleskowy,我真的很喜歡這一個,但僅供參考,這隻會打印主陣列1中不匹配的陣列而不是主陣列2中相應的不匹配陣列。 – WisdmLabs

+0

@WisdmLabs當然,就像所有的array_diff函數一樣將。以相反的順序傳遞數組,並且您擁有所有無與倫比的結果。 – Kleskowy

3
Your Arrays: 

$arr1 = array(
      array('name' => 'Alan', 'age' => '34', 'country' => 'usa'), 
      array('name' => 'James', 'age' => '24', 'country' => 'spain'), 
      ); 

$arr2 = array(
      array('name' => 'Alan', 'age' => '34', 'country' => 'usa'), 
      array('name' => 'James', 'age' => '54', 'country' => 'spffain'), 
     ); 



foreach($arr1 as $key=>$arr) 
{ 
$bool = true; 
$ar1 = $arr; 
$ar2 = $arr2[$key]; 

foreach($ar1 as $ky=>$val) 
{ 
    if($val != $ar2[$ky]) 
    { 
    $bool = false; 
    break; 
    } 
} 

if(!$bool) 
{ 
    echo "Unmatched Arrays: \r\n"; 
    print_r($ar1); echo " in Main Array 1 & \r\n"; 
    print_r($ar2); echo " in Main Array 2. \r\n"; 
} 

} 
+0

你的代碼仍然返回我匹配的數組。感謝您的努力.. –

+0

親愛的,布爾條件是不正確的。現在試試!謝謝! – WisdmLabs

+0

@maxcheng這可能適用於示例提供的數據,但恕我直言的比較在這裏很薄弱,而且這些代碼並不是可重用的。如果你喜歡,請查看我的答案。 – Kleskowy

2

試試這個

$arr1 = array(
     array('name' => 'Alan', 'age' => '34', 'country' => 'usa'), 
     array('name' => 'James', 'age' => '24', 'country' => 'spain'), 
     ); 

$arr2 = array(
     array('name' => 'Alan', 'age' => '34', 'country' => 'usa'), 
     array('name' => 'James', 'age' => '54', 'country' => 'spffain'), 
    ); 


$tmpArray = array(); 

foreach($arr1 as $data1) { 

    $duplicate = false; 
    foreach($arr2 as $data2) { 
    if($data1['name'] === $data2['name'] && $data1['age'] === $data2['age'] && $data1['country'] === $data2['country']) $duplicate = true; 
    } 

if($duplicate === false) $tmpArray[] = $data1; 
} 
echo "<pre>", print_r($tmpArray); 
0

我想給你一個可能的解決方案。我檢查了代碼,它似乎符合您的需求。 !

/** @var array $arr1 */ 
    $arr1 = array(
      array(
        'name' => 'Alan', 
        'age'  => '34', 
        'country' => 'usa' 
      ), 
      array(
        'name' => 'James', 
        'age'  => '24', 
        'country' => 'spain' 
      ), 
    ); 

    /** @var array $arr2 */ 
    $arr2 = array(
      array(
        'name' => 'Alan', 
        'age'  => '34', 
        'country' => 'usa' 
      ), 
      array(
        'name' => 'James', 
        'age'  => '54', 
        'country' => 'spffain' 
      ), 
    ); 

    /** @var array $notMatched */ 
    $notMatched = array(); 
    /** 
    * @var int $index 
    * @var array $element 
    */ 
    foreach($arr1 as $index => $element) { 
     /** 
     * @var string $key 
     * @var string $value 
     */ 
     foreach($element as $key => $value) { 
      if($arr2[$index][$key] !== $value) { 
       $notMatched["arr1"] = $arr1[$index]; 
       $notMatched["arr2"] = $arr2[$index]; 
      } 
     } 
    } 
    if(!empty($notMatched)) { 
     echo "Unmatched arrays: \r\n"; 
     echo "Array 1:\r\n"; 
     print_r($notMatched["arr1"]); 
     echo "Array 2:\r\n"; 
     print_r($notMatched["arr2"]); 
    } 
2
$intersect = array_uintersect($arr1, $arr2, 'compareDeepValue'); 
print_r($intersect); 
// add this and it will return the missing array. 
print_r(array_diff_key($arr1, $intersect)); 

function compareDeepValue($val1, $val2) 
{ 
    return strcmp($val1['age'], $val2['age']); 
    return strcmp($val1['country'], $val2['country']); 
    return strcmp($val1['name'], $val2['name']); 
}