function NestedArray::mergeDeep

Same name in other branches
  1. 9 core/lib/Drupal/Component/Utility/NestedArray.php \Drupal\Component\Utility\NestedArray::mergeDeep()
  2. 8.9.x core/lib/Drupal/Component/Utility/NestedArray.php \Drupal\Component\Utility\NestedArray::mergeDeep()
  3. 11.x core/lib/Drupal/Component/Utility/NestedArray.php \Drupal\Component\Utility\NestedArray::mergeDeep()

Merges multiple arrays, recursively, and returns the merged array.

This function is similar to PHP's array_merge_recursive() function, but it handles non-array values differently. When merging values that are not both arrays, the latter value replaces the former rather than merging with it.

Example:

$link_options_1 = [
    'fragment' => 'x',
    'attributes' => [
        'title' => t('X'),
        'class' => [
            'a',
            'b',
        ],
    ],
];
$link_options_2 = [
    'fragment' => 'y',
    'attributes' => [
        'title' => t('Y'),
        'class' => [
            'c',
            'd',
        ],
    ],
];
// This results in ['fragment' => ['x', 'y'], 'attributes' => ['title' => [t('X'), t('Y')], 'class' => ['a', 'b', 'c', 'd']]].
$incorrect = array_merge_recursive($link_options_1, $link_options_2);
// This results in ['fragment' => 'y', 'attributes' => ['title' => t('Y'), 'class' => ['a', 'b', 'c', 'd']]].
$correct = NestedArray::mergeDeep($link_options_1, $link_options_2);

Parameters

array ...: Arrays to merge.

Return value

array The merged array.

See also

NestedArray::mergeDeepArray()

83 calls to NestedArray::mergeDeep()
ArgumentPluginBase::calculateDependencies in core/modules/views/src/Plugin/views/argument/ArgumentPluginBase.php
Calculates dependencies for the configured plugin.
Attribute::merge in core/lib/Drupal/Core/Template/Attribute.php
Merges an Attribute object into the current storage.
AttributeHelper::mergeCollections in core/lib/Drupal/Core/Template/AttributeHelper.php
Merges two attribute collections.
BlockPluginTrait::setConfiguration in core/lib/Drupal/Core/Block/BlockPluginTrait.php
ClientFactory::fromOptions in core/lib/Drupal/Core/Http/ClientFactory.php
Constructs a new client object from some configuration.

... See full list

File

core/lib/Drupal/Component/Utility/NestedArray.php, line 297

Class

NestedArray
Provides helpers to perform operations on nested arrays and array keys of variable depth.

Namespace

Drupal\Component\Utility

Code

public static function mergeDeep() {
    return self::mergeDeepArray(func_get_args());
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.