recursive array_diff()?
There is one such function implemented in the comments of array_diff.
function arrayRecursiveDiff($aArray1, $aArray2) {
$aReturn = array();
foreach ($aArray1 as $mKey => $mValue) {
if (array_key_exists($mKey, $aArray2)) {
if (is_array($mValue)) {
$aRecursiveDiff = arrayRecursiveDiff($mValue, $aArray2[$mKey]);
if (count($aRecursiveDiff)) { $aReturn[$mKey] = $aRecursiveDiff; }
} else {
if ($mValue != $aArray2[$mKey]) {
$aReturn[$mKey] = $mValue;
}
}
} else {
$aReturn[$mKey] = $mValue;
}
}
return $aReturn;
}
The implementation only handles two arrays at a time, but I do not think that really posses a problem. You could run the diff sequentially if you need the diff of 3 or more arrays at a time. Also this method uses key checks and does a loose verification.
Recursive Difference of Arrays with Changed Array Order
You can achieve that by using array_diff
, array_column
and array_map
:
$col = "projectId";
$res = array_diff(array_column($a, $col), array_column($b, $col));
$res = array_map(function($id) use ($col) {return [$col => $id];}, $res);
Reference: array-diff, array-column, array_map
Live example: 3v4l
PHP array diff recursively (multidimensional) only on the keys
function array_key_recursive_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 comparison because matching keys were compared in the
// left-to-right loop earlier, recursively.
}
return $diff;
}
Multidimensional Array Diff Recursive not working in PHP
Apart from the fact that this could probably be solved in one sql query, here is a way of using php built in functions. Which are much faster than anything you can write.
When fetching data from DB use stone_id
s as keys
$allMilestone[$row['stone_id']] = $row;
// and
$checkedMilestone[$row['stone_id']] = $row;
or something like this. I am not fluent in php mysql driver use, but there probably is a function that returns associaltive array. If not than write out your select statements properly like SELECT stone_id, ... FROM ...
and use:
$allMilestone[$row[0]] = $row;
// and
$checkedMilestone[$row[0]] = $row;
Later you can use array_diff_key() function to have ... the difference !
array_diff not always correct
This is working correct. According to array_diff()
documentation:
array array_diff ( array $array1 , array $array2 [, array $... ] )
Compares array1 against one or more other arrays and returns the
values in array1 that are not present in any of the other arrays.
Another important info from documentation:
Two elements are considered equal if and only if (string) $elem1 ===
(string) $elem2. In words: when the string representation is the same.
So in $answers
array there are no k, B, Y
elements of $fields
array.
Why does array_diff on arrays of arrays return an empty array?
From the documentation:
Two elements are considered equal if and only if
(string) $elem1 === (string) $elem2
. In words: when the string representation is the same.
echo (string) array();
gives you just Array
, so for array_diff
, your arrays look like:
$array1 = array('Array', 'Array');
$array2 = array('Array');
So to create a diff for your arrays, you would need something like this (assuming that every element in the arrays is itself an array):
$diff = array();
foreach($array1 as $val1) {
$contained = false;
foreach($array2 as $val2) {
if(count(array_diff($val1, $val2)) == 0) {
$contained = true;
break;
}
}
if(!$contained) {
$diff[] = $val1;
}
}
Disclaimer: This is more or less just a sketch.
php compare two nested array and predict the difference
$results = array_diff(array_map('serialize',$a2),array_map('serialize',$a1));
$results = array_map('unserialize',$results);
echo '<pre>';
print_r($results);
Related Topics
How to Run a PHP File in a Scheduled Task (Windows Task Scheduler)
How to Iterate Over the Results in a MySQLi Result Set
PHP Connection Failed: Sqlstate[Hy000] [2002] Connection Refused
Convert/Cast an Stdclass Object to Another Class
Why Is PHP Session_Destroy() Not Working
Which $_Server Variables Are Safe
PHP Library For Parsing Xml With a Colons in Tag Names
PHP Image Upload Security Check List
Remove Script Tag from HTML Content
What Is the Best Collation to Use For MySQL With PHP
How to Prevent My Site Page to Be Loaded Via 3Rd Party Site Frame of Iframe
Get Everything Between ≪Tag≫ and ≪/Tag≫ With PHP
How to Remove Non-Alphanumeric Characters