You are here

public function Element::mergeCanadian in Lightning Core 8.4

Same name and namespace in other branches
  1. 8.5 src/Element.php \Drupal\lightning_core\Element::mergeCanadian()
  2. 8 src/Element.php \Drupal\lightning_core\Element::mergeCanadian()
  3. 8.2 src/Element.php \Drupal\lightning_core\Element::mergeCanadian()
  4. 8.3 src/Element.php \Drupal\lightning_core\Element::mergeCanadian()

Recursively merges arrays using the + method.

Existing keys at all levels of $a, both numeric and associative, will always be preserved. That's why I'm calling this a "Canadian" merge -- it does not want to step on any toes.

Parameters

array $a: The input array.

array $b: The array to merge into $a.

Return value

array The merged array.

File

src/Element.php, line 27

Class

Element
Helpful functions for dealing with renderable arrays and elements.

Namespace

Drupal\lightning_core

Code

public function mergeCanadian(array $a, array $b) {
  $a += $b;
  foreach ($a as $k => $v) {
    if (is_array($v) && isset($b[$k]) && is_array($b[$k])) {
      $a[$k] = static::mergeCanadian($a[$k], $b[$k]);
    }
  }
  return $a;
}