3 namespace Drupal\Component\Utility;
6 * Provides helpers to perform diffs on multi dimensional arrays.
13 * Recursively computes the difference of arrays with additional index check.
15 * This is a version of array_diff_assoc() that supports multidimensional
18 * @param array $array1
19 * The array to compare from.
20 * @param array $array2
21 * The array to compare to.
24 * Returns an array containing all the values from array1 that are not present
27 public static function diffAssocRecursive(array $array1, array $array2) {
30 foreach ($array1 as $key => $value) {
31 if (is_array($value)) {
32 if (!array_key_exists($key, $array2) || !is_array($array2[$key])) {
33 $difference[$key] = $value;
36 $new_diff = static::diffAssocRecursive($value, $array2[$key]);
37 if (!empty($new_diff)) {
38 $difference[$key] = $new_diff;
42 elseif (!array_key_exists($key, $array2) || $array2[$key] !== $value) {
43 $difference[$key] = $value;