Documentation

NestedArray
in package

Adapted from http://cgit.drupalcode.org/drupal/tree/core/lib/Drupal/Component/Utility/NestedArray.php @ f86a4d650d5af0b82a3981e09977055fa63f6f2e

Table of Contents

mergeDeep()  : array<string|int, mixed>
Merges multiple arrays, recursively, and returns the merged array.
mergeDeepArray()  : array<string|int, mixed>
Merges multiple arrays, recursively, and returns the merged array.

Methods

mergeDeep()

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

public static mergeDeep() : array<string|int, mixed>

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:

Tags
code

$link_options_1 = array('fragment' => 'x', 'attributes' => array('title' => t('X'), 'class' => array('a', 'b'))); $link_options_2 = array('fragment' => 'y', 'attributes' => array('title' => t('Y'), 'class' => array('c', 'd')));

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

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

endcode
see
NestedArray::mergeDeepArray()
Return values
array<string|int, mixed>

The merged array.

mergeDeepArray()

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

public static mergeDeepArray(array<string|int, mixed> $arrays[, bool $preserveIntegerKeys = false ]) : array<string|int, mixed>

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:

  • NestedArray::mergeDeep($a, $b);
  • NestedArray::mergeDeepArray(array($a, $b));

The following are also equivalent:

  • call_user_func_array('NestedArray::mergeDeep', $arrays_to_merge);
  • NestedArray::mergeDeepArray($arrays_to_merge);
Parameters
$arrays : array<string|int, mixed>

An arrays of arrays to merge.

$preserveIntegerKeys : bool = false

(optional) If given, integer keys will be preserved and merged instead of appended. Defaults to false.

Tags
see
NestedArray::mergeDeep()
Return values
array<string|int, mixed>

The merged array.

Search results