class NestedArray (View source)

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

Methods

static mixed
getValue(array $array, array $parents, bool $key_exists = NULL)

Retrieves a value from a nested array with variable depth.

static 
setValue(array $array, array $parents, mixed $value, bool $force = FALSE)

Sets a value in a nested array with variable depth.

static 
unsetValue(array $array, array $parents, bool $key_existed = NULL)

Unsets a value in a nested array with variable depth.

static bool
keyExists(array $array, array $parents)

Determines whether a nested array contains the requested keys.

static array
mergeDeep()

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

static array
mergeDeepArray(array $arrays, bool $preserve_integer_keys = FALSE)

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

static array
filter(array $array, callable $callable = NULL)

Filters a nested array recursively.

Details

static mixed getValue(array $array, array $parents, bool $key_exists = NULL)

Retrieves a value from a nested array with variable depth.

This helper function should be used when the depth of the array element being retrieved may vary (that is, the number of parent keys is variable). It is primarily used for form structures and renderable arrays.

Without this helper function the only way to get a nested array value with variable depth in one line would be using eval(), which should be avoided:

Parameters

array $array

The array from which to get the value.

array $parents

An array of parent keys of the value, starting with the outermost key.

bool $key_exists

(optional) If given, an already defined variable that is altered by reference.

Return Value

mixed

The requested nested value. Possibly NULL if the value is NULL or not all nested parent keys exist. $key_exists is altered by reference and is a Boolean that indicates whether all nested parent keys exist (TRUE) or not (FALSE). This allows to distinguish between the two possibilities when NULL is returned.

See also

NestedArray::setValue
NestedArray::unsetValue

static setValue(array $array, array $parents, mixed $value, bool $force = FALSE)

Sets a value in a nested array with variable depth.

This helper function should be used when the depth of the array element you are changing may vary (that is, the number of parent keys is variable). It is primarily used for form structures and renderable arrays.

Example:

Parameters

array $array

A reference to the array to modify.

array $parents

An array of parent keys, starting with the outermost key.

mixed $value

The value to set.

bool $force

(optional) If TRUE, the value is forced into the structure even if it requires the deletion of an already existing non-array parent value. If FALSE, PHP throws an error if trying to add into a value that is not an array. Defaults to FALSE.

See also

NestedArray::unsetValue
NestedArray::getValue

static unsetValue(array $array, array $parents, bool $key_existed = NULL)

Unsets a value in a nested array with variable depth.

This helper function should be used when the depth of the array element you are changing may vary (that is, the number of parent keys is variable). It is primarily used for form structures and renderable arrays.

Example:

Parameters

array $array

A reference to the array to modify.

array $parents

An array of parent keys, starting with the outermost key and including the key to be unset.

bool $key_existed

(optional) If given, an already defined variable that is altered by reference.

See also

NestedArray::setValue
NestedArray::getValue

static bool keyExists(array $array, array $parents)

Determines whether a nested array contains the requested keys.

This helper function should be used when the depth of the array element to be checked may vary (that is, the number of parent keys is variable). See NestedArray::setValue() for details. It is primarily used for form structures and renderable arrays.

If it is required to also get the value of the checked nested key, use NestedArray::getValue() instead.

If the number of array parent keys is static, this helper function is unnecessary and the following code can be used instead:

Parameters

array $array

The array with the value to check for.

array $parents

An array of parent keys of the value, starting with the outermost key.

Return Value

bool

TRUE if all the parent keys exist, FALSE otherwise.

See also

NestedArray::getValue

static array mergeDeep()

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

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:

Return Value

array

The merged array.

See also

NestedArray::mergeDeepArray

static array mergeDeepArray(array $arrays, bool $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:

  • 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

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

static array filter(array $array, callable $callable = NULL)

Filters a nested array recursively.

Parameters

array $array

The filtered nested array.

callable $callable

The callable to apply for filtering.

Return Value

array

The filtered array.