Multidimensional Array Difference PHP

multidimensional array difference php

Please check if I understand you correctly then this code snippet can help to you solve your problem. I have tested it for your specified problem only. if there are other testcases for which you want to run this, you can tell me to adjust the code.

$a1 = array(
'a1' => array('a_name' => 'aaa', 'a_value' => 'aaaaa'),
'b1' => array('b_name' => 'bbb', 'b_value' => 'bbbbbb'),
'c1' => array('c_name' => 'ccc', 'c_value' => 'cccccc')
);

$a2 = array(
'b1' => array('b_name' => 'zzzzz'),
);

$result = check_diff_multi($a1, $a2);
print '<pre>';
print_r($result);
print '</pre>';

function check_diff_multi($array1, $array2){
$result = array();
foreach($array1 as $key => $val) {
if(isset($array2[$key])){
if(is_array($val) && $array2[$key]){
$result[$key] = check_diff_multi($val, $array2[$key]);
}
} else {
$result[$key] = $val;
}
}

return $result;
}

EDIT: added tweak to code.

Compare 2-dimensional data sets based on a specified second level value

I would probably iterate through the original arrays and make them 1-dimensional... something like

foreach($array1 as $aV){
$aTmp1[] = $aV['ITEM'];
}

foreach($array2 as $aV){
$aTmp2[] = $aV['ITEM'];
}

$new_array = array_diff($aTmp1,$aTmp2);

How to get the difference of two multidimensional arrays in php?

Simply make a custom function like as

function check_diff($arr1, $arr2){
$check = (is_array($arr1) && count($arr1)>0) ? true : false;
$result = ($check) ? ((is_array($arr2) && count($arr2) > 0) ? $arr2 : array()) : array();
if($check){
foreach($arr1 as $key => $value){
if(isset($result[$key])){
$result[$key] = array_diff($value,$result[$key]);
}else{
$result[$key] = $value;
}
}
}

return $result;
}
$result['qtr_final'] = check_diff($a1['qtr_selected'],$a2['qtr_completed']);
print_r($result);

PHP - Finding the difference between Two Multidimensional Arrays with different structures based on one value

If I understand the question correctly you want to return entries from the first array if the ID doesn't exist as a Footage_ID in the second array.

If that's the case you can use array_filter() to check if each ID from the first array exists in the array of Footage_ID's from the second, and if it doesn't, return the current array:

$o = array_filter($array1, function($a1) use ($array2) {
return ! in_array($a1['ID'], array_column($array2, 'Footage_ID'));
});

Here's a simplified demo

Compare multidimensional arrays in PHP

The simplest way I know:

$a == $b;

Note that you can also use the ===. The difference between them is:

  1. With Double equals ==, order is important:

    $a = array(0 => 'a', 1 => 'b');
    $b = array(1 => 'b', 0 => 'a');
    var_dump($a == $b); // true
    var_dump($a === $b); // false
  2. With Triple equals ===, types matter:

    $a = array(0, 1);
    $b = array('0', '1');
    var_dump($a == $b); // true
    var_dump($a === $b); // false

Reference: Array operators

Get difference between two multidimensional arrays by comparing rows

You could always start by reading the PHP manual.

For array_diff_assoc (on http://php.net/manual/en/function.array-diff-assoc.php) it is said that this function only checks one dimension of a n-dimensional array. Of course you can check deeper dimensions by using, for example, array_diff_assoc($array1[0], $array2[0]);.

Provided solution in user comments (this does work):

55 dot php at imars dot com
17-Mar-2009 03:09 I've worked on
array_diff_assoc_recursive() mentioned
by chinello at gmail dot com and I
think it might be worth mentioning
here. I wrote a dozen test cases and
it seems to be holding up pretty well.

<?php
// dwarven Differences:
// * Replaced isset() with array_key_exists() to account for keys with null contents

// 55 dot php at imars dot com Differences:
// Key differences:
// * Removed redundant test;
// * Returns false bool on exact match (not zero integer);
// * Use type-precise comparison "!==" instead of loose "!=";
// * Detect when $array2 contains extraneous elements;
// * Returns "before" and "after" instead of only "before" arrays on mismatch.

function array_compare($array1, $array2) {
$diff = false;
// Left-to-right
foreach ($array1 as $key => $value) {
if (!array_key_exists($key,$array2)) {
$diff[0][$key] = $value;
} elseif (is_array($value)) {
if (!is_array($array2[$key])) {
$diff[0][$key] = $value;
$diff[1][$key] = $array2[$key];
} else {
$new = array_compare($value, $array2[$key]);
if ($new !== false) {
if (isset($new[0])) $diff[0][$key] = $new[0];
if (isset($new[1])) $diff[1][$key] = $new[1];
};
};
} elseif ($array2[$key] !== $value) {
$diff[0][$key] = $value;
$diff[1][$key] = $array2[$key];
};
};
// Right-to-left
foreach ($array2 as $key => $value) {
if (!array_key_exists($key,$array1)) {
$diff[1][$key] = $value;
};
// No direct comparsion because matching keys were compared in the
// left-to-right loop earlier, recursively.
};
return $diff;
};
?>

Get difference between associative rows of two 2-dimensional arrays

To check multi-deminsions try something like this:

$pageWithNoChildren = array_map('unserialize',
array_diff(array_map('serialize', $pageids), array_map('serialize', $parentpage)));
  • array_map() runs each sub-array of the main arrays through serialize() which converts each sub-array into a string representation of that sub-array
    • the main arrays now have values that are not arrays but string representations of the sub-arrays
  • array_diff() now has a one-dimensional array for each of the arrays to compare
  • after the difference is returned array_map() runs the array result (differences) through unserialize() to turn the string representations back into sub-arrays

Q.E.D.

Compare multidimensional arrays with array_diff

You could use array_udiff.

If will filter the first array, by comparing its elements to the elements of other arrays passed to array_udiff using the given callback. When the callback returns 0 for a pair, that element is removed from the result.

$result = array_udiff($arr2, $arr1, function ($a, $b) {
return strcmp($a['email'], $b['email']);
});

php two Multidimensional Array difference

Have you tried this?

for($i=0;$i<count($array1);$i++) {
$temp[$array1[$i]['F_CONTACT_ID']] = $array1[$i];
};

for($i=0;$i<count($array2);$i++) {
if($temp[$array2[$i]['F_CONTACT_ID']]) {
unset($temp[$array2[$i]['F_CONTACT_ID']]);
} else {
$temp[$array2[$i]['F_CONTACT_ID']] = $array2[$i];
}
}

echo "<pre>";
print_r($temp);
echo "</pre>";

The result will be some thing like this:

Array
(
[5] => Array
(
[F_CONTACT_ID] => 5
[F_CONTACT_FNAME] => name5
[F_CONTACT_NAME] => name55
)

)


Related Topics



Leave a reply



Submit