Jump to content

Multidimensional Array Diff


doublet216

Recommended Posts

I have a 2 dimensional array and I can't seem to get the difference between 2 arrays.

 

Array
(
[LineItem] => Array([1] => 1)
[Product] => Array([1] => 1)
)


Array
(
[Product] => Array([1] => 1)
)

 

$diff = array();

 

 foreach($group as $key => $value)
             {
          
                  foreach($group2 as $key2 $value2)
                  {                                   
                    if($value[$key1] == $value2[$key2])
                    continue 2;
                  }
             
                  $diff[] = $value;
                }

 

what i need is $diff to be

 

Array
(

[LineItem] => Array([1] => 1)
)

Link to comment
https://forums.phpfreaks.com/topic/275199-multidimensional-array-diff/
Share on other sites

$diff = array_diff_key($array1, $array2);

 

 

This will give you an array with all the keys in $array1 that were not present in $array2.

 

Ex:

 

$array1 = array('apple' => 'red', 'orange' => 'orange', 'lime' => 'green');
$array2 = array('apple' => 'green', 'lime' => 'yellow-ish green');

$diff = array_diff_key($array1, $array2);
print_r($diff);

 

This should print something like:

 

array
(
'orange' => 'orange'
)

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.