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 throughserialize()
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) throughunserialize()
to turn the string representations back into sub-arrays
Q.E.D.
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;
};
?>
While comparing associative rows between two 2d arrays, array_diff_assoc() gives the wrong difference
Two values from key => value pairs are considered equal only if
(string) $elem1 === (string) $elem2
. In other words a strict check takes place so the string representations must be the same.
http://php.net/manual/en/function.array-diff-assoc.php
The (string)
value of any array is "Array"
. Thus, your call to array_diff_assoc
is effectively comparing these two things:
Array ( [0] => "Array" [1] => "Array" )
Array ( [0] => "Array" )
Since the thing that is different between those two is the [1]
key/value pair from the first array, you get that back ([1] => Array( [11] => common set )
).
Check if associative multi-dimensional arrays are equal regardless of order of keys in either level
Here's a recursive comparison function
function CompareRecursive($array1, $array2, &$mismatches) {
foreach ($array1 as $key => $value) {
if (!isset($array2[$key])) {
$mismatches[$key] = [ $value ];
continue;
}
$value2 = $array2[$key];
if (!is_array($value) || !is_array($value2)) {
if ($value != $value2) {
$mismatches[$key] = [
$value, $value2
];
}
} else {
$mismatches_internal = [];
CompareRecursive($value, $value2, $mismatches_internal);
if (!empty($mismatches_internal)) {
$mismatches[$key] = $mismatches_internal;
}
}
}
return empty($mismatches);
}
As an added bonus this keeps track of mismatched entries too, there's a drawback when using this method that it won't work if $array2
has extra elements that $array1
doesn't but you can resolve this by doing:
$isEqual = CompareRecursive($array1,$array2) && CompareRecursive($array2,$array1);
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);
get the diffrence array from 2 multidimensional arrays
If you're looking at any difference and not just one value:
$result = array_map('unserialize', array_diff(
array_map('serialize', $array2), array_map('serialize', $array1)));
print_r($result);
Comparing Changes in Multidimensional Arrays - PHP
//'BEFORE' ARRAY
$arr1 = array( array(1231, 'Name1'), array(1232, 'Name2'), array(1233, 'Name3-Deleted') );
//'AFTER' ARRAY
$arr2 = array( array(1231, 'Name1-Renamed'), array(1232, 'Name2'), array(1230, 'Name4-New') );
// Kudos to AbraCadaver for the following:
$arr1 = array_column($arr1, 1, 0);
$arr2 = array_column($arr2, 1, 0);
$added = array_diff_key($arr2, $arr1);
$deleted = array_diff_key($arr1, $arr2);
$modified = array_diff_key(array_diff($arr2, $arr1), $deleted, $added);
Related Topics
PHP Is Confused When Adding and Concatenating
Login Without Https, How to Secure
How to Locate the PHP.Ini File (Xampp)
How to Change Envelope from Address Using PHP Mail
Multiple MySQL Insert Statements in One Query PHP
PHP Convert Date Format Dd/Mm/Yyyy =≫ Yyyy-Mm-Dd
How to Get the Sqlsrv Extension to Work With PHP, Since Mssql Is Deprecated
Use One Bind_Param() With Variable Number of Input Vars
Add a Custom Attribute to a Laravel/Eloquent Model on Load
PHP Append One Array to Another (Not Array_Push or +)
Convert Time and Date from One Time Zone to Another in PHP
PHP Fatal Error: Class 'Pdo' Not Found
PHP Regex: How to Match \R and \N Without Using [\R\N]
Multidimensional Array Iteration