You are here

public static function NestedArray::getValue in Blazy 7

Retrieves a value from a nested array with variable depth.

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()

2 calls to NestedArray::getValue()
NestedArray::keyExists in src/Utility/NestedArray.php
Determines whether a nested array contains the requested keys.
NestedArray::unsetValue in src/Utility/NestedArray.php
Unsets a value in a nested array with variable depth.

File

src/Utility/NestedArray.php, line 33

Class

NestedArray
Provides helpers to perform operations on nested arrays, kidnapped from D8.

Namespace

Drupal\blazy\Utility

Code

public static function &getValue(array &$array, array $parents, &$key_exists = NULL) {
  $ref =& $array;
  foreach ($parents as $parent) {
    if (is_array($ref) && (isset($ref[$parent]) || array_key_exists($parent, $ref))) {
      $ref =& $ref[$parent];
    }
    else {
      $key_exists = FALSE;
      $null = NULL;
      return $null;
    }
  }
  $key_exists = TRUE;
  return $ref;
}