From bc372e953794d8ec1f3eec3cb8d3a3217c596303 Mon Sep 17 00:00:00 2001 From: Pieter Frenssen Date: Thu, 3 Jan 2013 22:13:26 +0100 Subject: [PATCH] Issue #1850798 by pfrenssen: Provide a recursive version of array_diff_assoc(). --- core/includes/utility.inc | 36 ++++++++++++++++++++++++++++++++++++ 1 files changed, 36 insertions(+), 0 deletions(-) diff --git a/core/includes/utility.inc b/core/includes/utility.inc index 5019852..6eb8f55 100644 --- a/core/includes/utility.inc +++ b/core/includes/utility.inc @@ -63,3 +63,39 @@ function drupal_var_export($var, $prefix = '') { return $output; } + +/** + * 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 array_diff_assoc_recursive(array $array1, array $array2) { + $difference = array(); + foreach ($array1 as $key => $value) { + if (is_array($value)) { + if (!isset($array2[$key]) || !is_array($array2[$key])) { + $difference[$key] = $value; + } + else { + $new_diff = array_diff_assoc_recursive($value, $array2[$key]); + if (!empty($new_diff)) { + $difference[$key] = $new_diff; + } + } + } + elseif (!isset($array2[$key]) || $array2[$key] != $value) { + $difference[$key] = $value; + } + } + return $difference; +} -- 1.7.4.1