comparison core/lib/Drupal/Component/Utility/DiffArray.php @ 0:4c8ae668cc8c

Initial import (non-working)
author Chris Cannam
date Wed, 29 Nov 2017 16:09:58 +0000
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:4c8ae668cc8c
1 <?php
2
3 namespace Drupal\Component\Utility;
4
5 /**
6 * Provides helpers to perform diffs on multi dimensional arrays.
7 *
8 * @ingroup utility
9 */
10 class DiffArray {
11
12 /**
13 * Recursively computes the difference of arrays with additional index check.
14 *
15 * This is a version of array_diff_assoc() that supports multidimensional
16 * arrays.
17 *
18 * @param array $array1
19 * The array to compare from.
20 * @param array $array2
21 * The array to compare to.
22 *
23 * @return array
24 * Returns an array containing all the values from array1 that are not present
25 * in array2.
26 */
27 public static function diffAssocRecursive(array $array1, array $array2) {
28 $difference = [];
29
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;
34 }
35 else {
36 $new_diff = static::diffAssocRecursive($value, $array2[$key]);
37 if (!empty($new_diff)) {
38 $difference[$key] = $new_diff;
39 }
40 }
41 }
42 elseif (!array_key_exists($key, $array2) || $array2[$key] !== $value) {
43 $difference[$key] = $value;
44 }
45 }
46
47 return $difference;
48 }
49
50 }