From 04b47574eb50331b340eccc8610c1cff0f616d57 Mon Sep 17 00:00:00 2001 From: Clemens Schwaighofer Date: Wed, 4 Dec 2019 12:12:43 +0900 Subject: [PATCH] CoreLibs\Basic arrayDiff for full array diff PHP array_diff only compares missing elements in the second and existing in the first so a full diff is only achieved if compares both ways (a,b) and (b,a) This function uns a full compare and returns difference in an array --- www/lib/CoreLibs/Basic.php | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/www/lib/CoreLibs/Basic.php b/www/lib/CoreLibs/Basic.php index ce0de97b..80e20ed2 100644 --- a/www/lib/CoreLibs/Basic.php +++ b/www/lib/CoreLibs/Basic.php @@ -1390,6 +1390,21 @@ class Basic return $merged; } + /** + * correct array_diff that does an actualy difference between two arrays. + * array_diff only checks elements from A that are not in B, but not the + * other way around. + * Note that like array_diff this only checks first level values not keys + * @param array $a array to compare a + * @param array $b array to compare b + * @return array array with missing elements from a & b + */ + public static function arrayDiff(array $a, array $b): array + { + $intersect = array_intersect($a, $b); + return array_merge(array_diff($a, $intersect), array_diff($b, $intersect)); + } + /** * search for the needle array elements in haystack and return the ones found as an array, * is there nothing found, it returns FALSE (boolean)