public function Data::flatten

Converts a nested data array into a flattened structure with a combined key.

This function can be used by translators to help with the data conversion.

Nested keys will be joined together using a colon, so for example $data['key1']['key2']['key3'] will be converted into $flattened_data['key1][key2][key3'].

Parameters

array $data: The nested array structure that should be flattened.

string $prefix: Internal use only, indicates the current key prefix when recursing into the data array.

array $label: Label for the data.

Return value

array The flattened data array.

2 calls to Data::flatten()
Data::filterTranslatable in src/Data.php
Flattens and filters data for being translatable.
Data::getTranslatableFiles in src/Data.php
Returns a list of translatable files.

File

src/Data.php, line 58

Class

Data
All data-related functions.

Namespace

Drupal\tmgmt

Code

public function flatten(array $data, $prefix = NULL, $label = array()) {
  $flattened_data = array();
  if (isset($data['#label'])) {
    $label[] = $data['#label'];
  }

  // Each element is either a text (has #text property defined) or has children,
  // not both.
  if (!empty($data['#text']) || !empty($data['#file'])) {
    $flattened_data[$prefix] = $data;
    $flattened_data[$prefix]['#parent_label'] = $label;
  }
  else {
    $prefix = isset($prefix) ? $prefix . static::TMGMT_ARRAY_DELIMITER : '';
    foreach (Element::children($data) as $key) {
      $flattened_data += $this
        ->flatten($data[$key], $prefix . $key, $label);
    }
  }
  return $flattened_data;
}