Skip to content
Snippets Groups Projects
NestedArray.php 1.26 KiB
Newer Older
  • Learn to ignore specific revisions
  • <?php
    
    namespace LakeDrops\Docker4Drupal;
    
    
    jurgenhaas's avatar
    jurgenhaas committed
    /**
     * Class NestedArray.
     *
     * @package LakeDrops\Docker4Drupal
     */
    
    class NestedArray {
    
    
    jurgenhaas's avatar
    jurgenhaas committed
      /**
       * Deeply merges arrays. Borrowed from drupal.org/project/core.
       *
       * @return array
       *   The merged array.
       */
    
      public static function mergeDeep(): array {
    
        return self::mergeDeepArray(func_get_args());
      }
    
    
    jurgenhaas's avatar
    jurgenhaas committed
      /**
       * Deeply merges arrays. Borrowed from drupal.org/project/core.
       *
       * @param array $arrays
       *   An array of array that will be merged.
       * @param bool $preserve_integer_keys
       *   Whether to preserve integer keys.
       *
       * @return array
       *   The merged array.
       */
    
      public static function mergeDeepArray(array $arrays, $preserve_integer_keys = FALSE): array {
    
        $result = [];
        foreach ($arrays as $array) {
          foreach ($array as $key => $value) {
            if (is_int($key) && !$preserve_integer_keys) {
              $result[] = $value;
            }
    
            /** @noinspection NotOptimalIfConditionsInspection */
    
            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;
      }
    
    }