diff --git a/core/lib/Drupal/Core/Utility/Token.php b/core/lib/Drupal/Core/Utility/Token.php
index a250d5892b..40e576708a 100644
--- a/core/lib/Drupal/Core/Utility/Token.php
+++ b/core/lib/Drupal/Core/Utility/Token.php
@@ -225,6 +225,70 @@ public function replace($text, array $data = [], array $options = [], Bubbleable
   }
 
   /**
+   * Replaces all tokens in a given array with appropriate values for both key
+   * and value.
+   *
+   * @param array $array
+   *   An array containing replaceable tokens. The caller is responsible for
+   *   calling \Drupal\Component\Utility\Html::escape() in case the key-value
+   *   pairs were plain text.
+   * @param array $data
+   *   (optional) An array of keyed objects. For simple replacement scenarios
+   *   'node', 'user', and others are common keys, with an accompanying node or
+   *   user object being the value. Some token types, like 'site', do not require
+   *   any explicit information from $data and can be replaced even if it is
+   *   empty.
+   * @param array $options
+   *   (optional) A keyed array of settings and flags to control the token
+   *   replacement process. Supported options are:
+   *   - langcode: A language code to be used when generating locale-sensitive
+   *     tokens.
+   *   - callback: A callback function that will be used to post-process the
+   *     array of token replacements after they are generated.
+   *   - clear: A boolean flag indicating that tokens should be removed from the
+   *     final text if no replacement value can be generated.
+   * @param \Drupal\Core\Render\BubbleableMetadata $bubbleable_metadata|null
+   *   (optional) An object to which static::generate() and the hooks and
+   *   functions that it invokes will add their required bubbleable metadata.
+   *
+   *   To ensure that the metadata associated with the token replacements gets
+   *   attached to the same render array that contains the token-replaced text,
+   *   callers of this method are encouraged to pass in a BubbleableMetadata
+   *   object and apply it to the corresponding render array. For example:
+   *
+   *   @code
+   *     $bubbleable_metadata = new BubbleableMetadata();
+   *     $build['#markup'] = $token_service->replace('Tokens: [node:nid] [current-user:uid]', ['node' => $node], [], $bubbleable_metadata);
+   *     $bubbleable_metadata->applyTo($build);
+   *   @endcode
+   *
+   *   When the caller does not pass in a BubbleableMetadata object, this
+   *   method creates a local one, and applies the collected metadata to the
+   *   Renderer's currently active render context.
+   *
+   * @return array
+   *   The token result is the entered array with tokens replaced for both key
+   *   and value. The caller is responsible for choosing the right
+   *   escaping / sanitization. If the result is intended to be used as plain
+   *   text, using PlainTextOutput::renderFromHtml() is recommended. If the
+   *   result is just printed as part of a template relying on Twig autoescaping
+   *   is possible, otherwise for example the result can be put into #markup, in
+   *   which case it would be sanitized by Xss::filterAdmin().
+   */
+  public function replaceArray(array $array, array $data = [], array $options = [], BubbleableMetadata $bubbleable_metadata = NULL) {
+    foreach ($array as $key => $value) {
+      $key1 = $this->replace($key, $data, $options, $bubbleable_metadata);
+      $value1 = $this->replace($value, $data, $options, $bubbleable_metadata);
+
+      // Replace the key and value in the array.
+      unset($array[$key]);
+      $array[$key1] = $value1;
+    }
+
+    return $array;
+  }
+
+  /**
    * Builds a list of all token-like patterns that appear in the text.
    *
    * @param string $text
