Skip to main content
Drupal API
User account menu
  • Log in

Breadcrumb

  1. Drupal Core 11.1.x
  2. DiffArray.php

class DiffArray

Provides helpers to perform diffs on multi dimensional arrays.

Hierarchy

  • class \Drupal\Component\Utility\DiffArray

Expanded class hierarchy of DiffArray

Related topics

Utility classes and functions
Overview of utility classes and functions for developers.
2 files declare their use of DiffArray
HTMLRestrictions.php in core/modules/ckeditor5/src/HTMLRestrictions.php
Link.php in core/modules/jsonapi/src/JsonApiResource/Link.php

File

core/lib/Drupal/Component/Utility/DiffArray.php, line 10

Namespace

Drupal\Component\Utility
View source
class DiffArray {
    
    /**
     * 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.
     */
    public static function diffAssocRecursive(array $array1, array $array2) {
        $difference = [];
        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 = static::diffAssocRecursive($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;
    }

}

Members

Title Sort descending Modifiers Object type Summary
DiffArray::diffAssocRecursive public static function Recursively computes the difference of arrays with additional index check.
RSS feed
Powered by Drupal