You are here

public static function WebformArrayHelper::insertBefore in Webform 8.5

Same name and namespace in other branches
  1. 6.x src/Utility/WebformArrayHelper.php \Drupal\webform\Utility\WebformArrayHelper::insertBefore()

Inserts a new key/value before the key in the array.

Parameters

array &$array: An array to insert in to.

string $target_key: The key to insert before.

string $new_key: The key to insert.

mixed $new_value: An value to insert.

File

src/Utility/WebformArrayHelper.php, line 347

Class

WebformArrayHelper
Provides helper to operate on arrays.

Namespace

Drupal\webform\Utility

Code

public static function insertBefore(array &$array, $target_key, $new_key, $new_value) {
  $new = [];
  foreach ($array as $k => $value) {
    if ($k === $target_key) {
      $new[$new_key] = $new_value;
    }
    $new[$k] = $value;
  }
  $array = $new;
}