2017-08-21 47 views
1

我需要找到多維數組之間的差異多維數組的區別

我有數組是這樣的:

$arr1 = array(
     'test1' => array( 
      'XXX' => array(
       'w1'  => array('id' =>  '5'), 
       'w2'  => array('id' =>  '2'), 
       'w3'  => array('id' =>  'g'), 
      ), 
      'YYY' => array(
       'w1'  => array('id' =>  '4'), 
       'w2'  => array('id' =>  '1') 
      ), 
      'ZZZ' => array(
       'w1'  => array('id' =>  '3'), 
       'w2'  => array('id' =>  '9') 
      ), 
      'QQQ' => array(
       'w1'  => array('id' =>  '3'), 
       'w2'  => array('id' =>  '9') 
      ),    
     ), 
     'test2' => array( 
      'XXX' => array(
       'w1'  => array('id' =>  '8'), 
       'w2'  => array('id' =>  '3') 
      ), 
      'YYY' => array(
       'w1'  => array('id' =>  '15'), 
       'w2'  => array('id' =>  '1') 
      ), 
      'ZZZ' => array(
       'w1'  => array('id' =>  '5'), 
       'w2'  => array('id' =>  '2') 
      ),   
     ), 
     ); 


$arr2 = array(
     'test1' => array(
      'XXX' => array(
       'w1'  => array('id' =>  '5'), 
       'w2'  => array('id' =>  '2'), 
       'w3'  => array('id' =>  'g'), 
       'w4'  => array('id' =>  'x'), 
      ), 
      'YYY' => array(
       'w1'  => array('id' =>  '4'), 
       'w2'  => array('id' =>  '1') 
      ), 
      'ZZZ' => array(
       'w1'  => array('id' =>  '3'), 
       'w2'  => array('id' =>  '9') 
      ), 
     ), 
     );  

,我需要刪除重複,但我需要比第一級密鑰,二級密鑰和去年水平鍵,值對,所以我的結果應該是這樣的

array(
      'test1' => array(
       'XXX' => array(
        'w4'  => array('id' =>  'x'), 
       ), 
) 

我嘗試使用

function check_key($a,$b) { 

if ($a===$b) 
    { 
    return 0; 
    } 
    return ($a>$b)?1:-1;  
} 

function check_value($a,$b) { 

if ($a===$b) 
    { 
    return 0; 
} 
    return ($a>$b)?1:-1; 
} 

$merged_arr = array_udiff_uassoc($arr2,$arr1,"check_key","check_value"); 

但是這個函數不會比較上一級的最後一級鍵值對。 我會尋求幫助

回答

1

嘗試使用此功能。我認爲這將對您有所幫助。

$result[] = array_diff_key($arr1, $arr2); 
print_r($result);