NestedArray
class NestedArray (View source)
Provides helpers to perform operations on nested arrays and array keys of variable depth.
Methods
Retrieves a value from a nested array with variable depth.
Sets a value in a nested array with variable depth.
Unsets a value in a nested array with variable depth.
Determines whether a nested array contains the requested keys.
Merges multiple arrays, recursively, and returns the merged array.
Merges multiple arrays, recursively, and returns the merged array.
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:
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:
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:
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:
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:
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);
static array
filter(array $array, callable $callable = NULL)
Filters a nested array recursively.