php - Multidimensional array difference

830

I need find difference between multidimensional arrays

I have arrays like this :

$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')
           ),
         ),
       );    

And I need to remove duplicates but I need compare first level key, second level key and last level key,value pairs so my results should be like that

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

I try use

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"); 

But this function don't compare last level key-value pair in last level .

176

Answer

Solution:

Try with this function.I think It will be helpful to you.

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

People are also looking for solutions to the problem: php - Two or more forms on 1 page doesn't post images

Source

Didn't find the answer?

Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.

Ask a Question

Write quick answer

Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.

Similar questions

Find the answer in similar questions on our website.