summaryrefslogtreecommitdiff
path: root/includes/common.inc
diff options
context:
space:
mode:
authorDavid Rothstein <drothstein@gmail.com>2013-06-23 17:17:12 -0400
committerDavid Rothstein <drothstein@gmail.com>2013-06-23 17:17:12 -0400
commitf083e0f782cf424c3b93b4c3cbacd51bfb635e5f (patch)
tree29b90b556496afa1dad3b156dee62c1bf212968e /includes/common.inc
parent3201287ba449a71ea01c2159b59b71a020617ea5 (diff)
downloadbrdo-f083e0f782cf424c3b93b4c3cbacd51bfb635e5f.tar.gz
brdo-f083e0f782cf424c3b93b4c3cbacd51bfb635e5f.tar.bz2
Issue #1850798 by damiankloip, ACF, geerlingguy, pfrenssen, David_Rothstein | mvc: Add a recursive version of array_diff_assoc().
Diffstat (limited to 'includes/common.inc')
-rw-r--r--includes/common.inc38
1 files changed, 38 insertions, 0 deletions
diff --git a/includes/common.inc b/includes/common.inc
index 3ec68636c..9283766c2 100644
--- a/includes/common.inc
+++ b/includes/common.inc
@@ -6470,6 +6470,44 @@ function element_set_attributes(array &$element, array $map) {
}
/**
+ * Recursively computes the difference of arrays with additional index check.
+ *
+ * This is a version of array_diff_assoc() that supports multidimensional
+ * arrays.
+ *
+ * @param array $array1
+ * The array to compare from.
+ * @param array $array2
+ * The array to compare to.
+ *
+ * @return array
+ * Returns an array containing all the values from array1 that are not present
+ * in array2.
+ */
+function drupal_array_diff_assoc_recursive($array1, $array2) {
+ $difference = array();
+
+ foreach ($array1 as $key => $value) {
+ if (is_array($value)) {
+ if (!array_key_exists($key, $array2) || !is_array($array2[$key])) {
+ $difference[$key] = $value;
+ }
+ else {
+ $new_diff = drupal_array_diff_assoc_recursive($value, $array2[$key]);
+ if (!empty($new_diff)) {
+ $difference[$key] = $new_diff;
+ }
+ }
+ }
+ elseif (!array_key_exists($key, $array2) || $array2[$key] !== $value) {
+ $difference[$key] = $value;
+ }
+ }
+
+ return $difference;
+}
+
+/**
* Sets a value in a nested array with variable depth.
*
* This helper function should be used when the depth of the array element you