function NestedArray::keyExists

Same name in other branches
  1. 9 core/lib/Drupal/Component/Utility/NestedArray.php \Drupal\Component\Utility\NestedArray::keyExists()
  2. 10 core/lib/Drupal/Component/Utility/NestedArray.php \Drupal\Component\Utility\NestedArray::keyExists()
  3. 11.x core/lib/Drupal/Component/Utility/NestedArray.php \Drupal\Component\Utility\NestedArray::keyExists()

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:

$value_exists = isset($form['signature_settings']['signature']);
$key_exists = array_key_exists('signature', $form['signature_settings']);

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

6 calls to NestedArray::keyExists()
Config::hasOverrides in core/lib/Drupal/Core/Config/Config.php
Determines if overrides are applied to a key for this configuration object.
FormBuilder::handleInputElement in core/lib/Drupal/Core/Form/FormBuilder.php
Adds the #name and #value properties of an input element before rendering.
FormValidator::handleErrorsWithLimitedValidation in core/lib/Drupal/Core/Form/FormValidator.php
Handles validation errors for forms with limited validation.
NestedArrayTest::testKeyExists in core/tests/Drupal/Tests/Component/Utility/NestedArrayTest.php
Tests existence of array key.
Row::hasDestinationProperty in core/modules/migrate/src/Row.php
Tests if destination property exists.

... See full list

File

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

Class

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

Namespace

Drupal\Component\Utility

Code

public static function keyExists(array $array, array $parents) {
    // Although this function is similar to PHP's array_key_exists(), its
    // arguments should be consistent with getValue().
    $key_exists = NULL;
    self::getValue($array, $parents, $key_exists);
    return $key_exists;
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.