You are here

public static function NestedArray::mergeDeepArray in Blazy 7

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

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()

1 call to NestedArray::mergeDeepArray()
NestedArray::mergeDeep in src/Utility/NestedArray.php
Merges multiple arrays, recursively, and returns the merged array.

File

src/Utility/NestedArray.php, line 172

Class

NestedArray
Provides helpers to perform operations on nested arrays, kidnapped from D8.

Namespace

Drupal\blazy\Utility

Code

public static function mergeDeepArray(array $arrays, $preserve_integer_keys = FALSE) {
  $result = [];
  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_int($key) && !$preserve_integer_keys) {
        $result[] = $value;
      }
      elseif (isset($result[$key]) && is_array($result[$key]) && is_array($value)) {
        $result[$key] = self::mergeDeepArray([
          $result[$key],
          $value,
        ], $preserve_integer_keys);
      }
      else {
        $result[$key] = $value;
      }
    }
  }
  return $result;
}