8.5.x NestedArray.php &NestedArray::getValue(array &$array, array $parents, &$key_exists = NULL)
8.0.x NestedArray.php &NestedArray::getValue(array &$array, array $parents, &$key_exists = NULL)
8.1.x NestedArray.php &NestedArray::getValue(array &$array, array $parents, &$key_exists = NULL)
8.2.x NestedArray.php &NestedArray::getValue(array &$array, array $parents, &$key_exists = NULL)
8.3.x NestedArray.php &NestedArray::getValue(array &$array, array $parents, &$key_exists = NULL)
8.4.x NestedArray.php &NestedArray::getValue(array &$array, array $parents, &$key_exists = NULL)
8.6.x NestedArray.php &NestedArray::getValue(array &$array, array $parents, &$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:

// Do not do this! Avoid eval().
// May also throw a PHP notice, if the variable array keys do not exist.
eval('$value = $array[\'' . implode("']['", $parents) . "'];");

Instead, use this helper function:

$value = NestedArray::getValue($form, $parents);

A return value of NULL is ambiguous, and can mean either that the requested key does not exist, or that the actual value is NULL. If it is required to know whether the nested array key actually exists, pass a third argument that is altered by reference:

$key_exists = NULL;
$value = NestedArray::getValue($form, $parents, $key_exists);
if ($key_exists) {

  // Do something with $value.
}

However if the number of array parent keys is static, the value should always be retrieved directly rather than calling this function. For instance:

$value = $form['signature_settings']['signature'];

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

46 calls to NestedArray::getValue()
ContentTranslationUpdatesManager::onMigrateImport in core/modules/content_translation/src/ContentTranslationUpdatesManager.php
Listener for migration imports.
Datelist::validateDatelist in core/lib/Drupal/Core/Datetime/Element/Datelist.php
Validation callback for a datelist element.
Datetime::validateDatetime in core/lib/Drupal/Core/Datetime/Element/Datetime.php
Validation callback for a datetime element.
EntityReferenceItem::settingsAjax in core/lib/Drupal/Core/Field/Plugin/Field/FieldType/EntityReferenceItem.php
Ajax callback for the handler settings form.
Extract::transform in core/modules/migrate/src/Plugin/migrate/process/Extract.php
Performs the associated process.

... See full list

File

core/lib/Drupal/Component/Utility/NestedArray.php, line 69

Class

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

Namespace

Drupal\Component\Utility

Code

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