W3cubDocs

/Drupal 8

public static function NestedArray::mergeDeepArray

public static NestedArray::mergeDeepArray(array $arrays, $preserve_integer_keys = FALSE)

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

This function is equivalent to NestedArray::mergeDeep(), except the input arrays are passed as a single array parameter rather than a variable parameter list.

The following are equivalent:

The following are also equivalent:

Parameters

array $arrays: An arrays of arrays to merge.

bool $preserve_integer_keys: (optional) If given, integer keys will be preserved and merged instead of appended. Defaults to FALSE.

Return value

array The merged array.

See also

NestedArray::mergeDeep()

File

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

Class

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

Namespace

Drupal\Component\Utility

Code

public static function mergeDeepArray(array $arrays, $preserve_integer_keys = FALSE) {
  $result = array();
  foreach ($arrays as $array) {
    foreach ($array as $key => $value) {
      // Renumber integer keys as array_merge_recursive() does unless
      // $preserve_integer_keys is set to TRUE. Note that PHP automatically
      // converts array keys that are integer strings (e.g., '1') to integers.
      if (is_integer($key) && !$preserve_integer_keys) {
        $result[] = $value;
      }
      // Recurse when both values are arrays.
      elseif (isset($result[$key]) && is_array($result[$key]) && is_array($value)) {
        $result[$key] = self::mergeDeepArray(array($result[$key], $value), $preserve_integer_keys);
      }
      // Otherwise, use the latter value, overriding any previous value.
      else {
        $result[$key] = $value;
      }
    }
  }
  return $result;
}

© 2001–2016 by the original authors
Licensed under the GNU General Public License, version 2 and later.
Drupal is a registered trademark of Dries Buytaert.
https://api.drupal.org/api/drupal/core!lib!Drupal!Component!Utility!NestedArray.php/function/NestedArray::mergeDeepArray/8.1.x